You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeca55ff8)错误?

Fixing the Thread 1: EXC_BAD_ACCESS Crash in UICollectionView Cell Registration

Hey there, let's break down why you're hitting this crash and how to fix it quickly!

The Root Cause

Your custom ThingsToDoCollectionView is overriding the register(_:forCellWithReuseIdentifier:) method, and inside that override, you're registering UICollectionView.self instead of your actual cell class ThingsToDoCollectionViewCell.

This is a critical mismatch—when the collection view tries to dequeue a cell, it's expecting a UICollectionViewCell subclass but gets a full UICollectionView instance instead. This invalid type cast leads directly to the EXC_BAD_ACCESS memory error you're seeing.

How to Fix It

  1. Remove the incorrect register method override from your ThingsToDoCollectionView class. You don't need to override this method unless you have very specific custom logic, which isn't the case here.
  2. Register your cell correctly in the initialization methods of your custom collection view (or in your view controller, if that's where you prefer to handle registration).

Here's the corrected code for your ThingsToDoCollectionView:

import UIKit
class ThingsToDoCollectionView: UICollectionView {
    // For code-initiated collection views
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, layout: layout)
        setupCellRegistration()
    }
    
    // For storyboard/xib-initiated collection views
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupCellRegistration()
    }
    
    private func setupCellRegistration() {
        // If your cell is created with a XIB (since you have IBOutlets)
        let cellNib = UINib(nibName: "ThingsToDoCollectionViewCell", bundle: nil)
        register(cellNib, forCellWithReuseIdentifier: "ThingsToDoCollectionViewCell")
        
        // If your cell is pure code-based, use this instead:
        // register(ThingsToDoCollectionViewCell.self, forCellWithReuseIdentifier: "ThingsToDoCollectionViewCell")
    }
}

Key Notes

  • Since your ThingsToDoCollectionViewCell uses IBOutlets, it's almost certainly created with a XIB file. Make sure the nibName matches exactly the name of your XIB file (case-sensitive!).
  • Avoid overriding the default register method unless you fully understand how it interacts with the collection view's cell reuse system—your original override was replacing any valid registration calls with an invalid one.

内容的提问来源于stack exchange,提问作者user10081458

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:25:34