我想实现一个集合视图控制器,并大量失败...我如何摆脱这个解包错误?它在我的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