我想實現一個集合視圖控制器,並大量失敗...我如何擺脫這個解包錯誤?它在我的cellForRowAtIndexPath函數中創建我的collectionView單元格時發生。UICollectionViewController - 意外發現零,同時展開一個可選值
這是代碼。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell
if let photoObject = pictureObjects.objectAtIndex(indexPath.row) as? PictureElement {
let title = photoObject.title ?? "" // if photoObject.title == nil, then we return an empty string
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(photoObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
let author = photoObject.author ?? ""
let issueNumber = photoObject.issueNumber ?? ""
let volumeNumber = photoObject.volumeNumber ?? ""
let nodeID = photoObject.nodeID ?? 0
let imageURL = photoObject.imageURL ?? ""
cell!.request?.cancel()
// Using image cache system to make sure the table view works even when rapidly scrolling down the screen.
if let image = self.imageCache.objectForKey(imageURL) as? UIImage {
cell!.imageView.image = image
} else {
cell!.imageView.image = nil
cell!.request = Alamofire.request(.GET, imageURL).responseImage { response in
if let image = response.result.value {
self.imageCache.setObject(response.result.value!, forKey: imageURL)
cell!.imageView.image = image
} else {
}
}
}
} else {
}
return cell!
}
而且,我已經嘗試過使用保護,讓語句,並沒有奏效。那就是:
guard let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell else {
print("cell could not be initialized as PhotoBrowserCollectionViewCell, hence it will be casted to another default UICollectionViewCell type")
return collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath)
}
需要更多信息,你可以發佈你的錯誤信息。 – Barrett
這就是我所能得到的。我一直無法找出其他的東西。你認爲這可能是什麼(即使你不確定)? –
哪一行導致錯誤? – t4nhpt