我正在使用最新稳定的 Swift 在我的函数中,当我运行我的 ios 应用程序时,我收到 fatal error 。
"fatal error: unexpectedly found nil while unwrapping an Optional value".
我寻找答案,但他们提供了不同的解包方法。
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])
}
最佳答案
您可以使用 if let block 来确保在尝试使用该值之前不为 nil
if let imageView = cell.viewWithTag(1) as? UIImageView {
imageView.image = UIImage(named: imagesArray[indexPath.row])
}
关于swift - 展开可选绑定(bind) fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808370/