2015-06-12 42 views
0

我正在使用最新穩定斯威夫特 裏面我的功能我得到致命錯誤。當我跑我的ios應用程序。展開可選綁定致命錯誤

"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]) 
     } 

回答

1

您可以使用如果讓塊,以確保該值不爲零嘗試使用它

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

謝謝你加入{將使其競爭解決方案之前,由於 – user554505

+0

如果你是使用自定義單元格並將數據填充到自定義單元格的子視圖中,刪除以下行//如果此類不是自定義類,則註冊單元類 self.collectionView!.registerClass(CollectionViewCell.self,forCellWithReuseIdentifier:reuseIdentifier) – cmp

相關問題