swift - 如何消除导致此线程 1 : EXC_BAD_ACCESS error? 的错误

标签 swift xcode debugging uicollectionviewcell exc-bad-access

我对 Swift 还很陌生(大约一个月前才学),我正在创建一个自定义 Collection View 单元格,并将我的自定义类连接到 View Controller 类。我问这个问题是因为我一直在努力解决一个错误

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        let answer = collectionView.numberOfItems(inSection: cellArray.count)

        return answer
}

出去好几个小时。这个错误是我的 View Controller 类中的一个方法,它在 LLDB 窗口中显示一条错误消息“Thread 1: EXC_BAD_ACCESS (code=2...[computer memory address here]” is highlighted at let answer = collectionView .nu​​mberOfItems(inSection: cellArray.count) 我有 followed this tutorial 但 bug 没有被消灭。

我已经包含了我的 View Controller 类的一部分,我在其中初始化了属性,如果这有帮助的话:

@IBOutlet weak var ibCollectionView: UICollectionView!

var cellArray:[customCollectionViewCell] = Array(repeatElement(customCollectionViewCell(), count: 6))

let displayedCellDimensions = CGSize(width: 343, height: 248)

LLDB界面截图: Here is a screenshot of the LLDB interface.

感谢任何帮助,因为我很困。谢谢!

最佳答案

首先让我吃惊的是,您正在递归调用您的 numberOfItemsInSection:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    let answer = collectionView.numberOfItems(inSection: cellArray.count)

    return answer

相反,你应该只给它想要的东西,元素的数量:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return cellArray.count

当您在 swift 文件中重命名或删除并重新添加 InterfaceBuilder 导出而不修改 Storyboard 导出时,会发生处理 collectionView 和 tableView 时的另一个问题。我附上了屏幕截图,以便您明白我的意思:

Storyboard, assistant editor, and connections inspector

如果其中一个实心圆圈是空的,则连接断开。要解决此问题:

  1. 打开 Storyboard并在层次结构中选择 Collection View 。
  2. CTRL+从层次结构拖动到助手编辑器中的 swift 文件(使用热键 ALT+CMD+Enter 打开)
  3. 应该会出现一个对话。输入 socket 的设置。
  4. 清理 swift 文件和连接编辑器中的所有旧 socket 和断开的连接。

关于swift - 如何消除导致此线程 1 : EXC_BAD_ACCESS error? 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591092/

相关文章:

ios - 如何在不同的行中将字符串写入 iOS 上的 .txt 文件

iphone - 在iOS中流畅播放声音

Xcode 5插件

ruby - 如何在 ruby​​-debug 控制台中评估多行 block

debugging - vb6 如何在 msgbox 中进入 Debug模式

swift - UICollectionView 单元格垂直排列而不是网格排列

swift - 泛型类不会调用重写的初始值设定项

ios - 如果打开谷歌地图或打开电话栏,LaunchScreen 中的标签显示不正确

scala - 调试用 Scala 编写的作为 sbt 插件的软件验证程序

Swift - 难以解释,数学函数很少