【问题标题】:Unwrapping optional binding fatal error展开可选绑定致命错误
【发布时间】:2015-06-12 16:52:13
【问题描述】:

我正在使用最新稳定的 Swift 在我的函数中,我得到 致命错误。当我运行我的 ios 应用程序时。

"致命错误:在展开 Optional 时意外发现 nil 价值”。

我搜索了答案,但他们提供了不同的解包方法。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
        var imageView = cell.viewWithTag(1) as! UIImageView
        imageView.image = UIImage(named: imagesArray[indexPath.row])
        }

【问题讨论】:

    标签: swift xcode6


    【解决方案1】:

    您可以在尝试使用之前使用 if let 块来确保该值不为零

    if let imageView = cell.viewWithTag(1) as? UIImageView {
        imageView.image = UIImage(named: imagesArray[indexPath.row])
    }
    

    【讨论】:

    • 感谢您添加 { 将使其成为一个有竞争力的解决方案,谢谢
    • 如果您正在使用自定义单元格并将数据填充到自定义单元格的子视图中,请删除以下行 // 如果这不是自定义类 self.collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: 则注册单元格类:重用标识符)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多