我正在构建一个Collection View来从CloudKit数据库返回图像。结果缓慢回归,但他们正在回归。但是,我得到了经典的“致命错误:在展开可选值时意外发现nil”错误,我无法弄清楚为什么或我需要修复以解决错误。有没有人对如何解决这个问题有任何建议?
我的代码如下:
class CollectionViewController: UICollectionViewController {
@IBOutlet var myCollectionView: UICollectionView!
var image = UIImage()
var matchedSelfie = [UIImage]()
let reuseIdentifier = "MyCell"
override func viewDidLoad() {
super.viewDidLoad()
// self.canDisplayBannerAds = true
println("Loading")
var countedImages = matchedSelfie
let publicDB = CKContainer.defaultContainer().publicCloudDatabase
print(publicDB)
let data = CKRecord(recordType: "theUsers")
let pred = NSPredicate(format: "TRUEPREDICATE")
let myQuery = CKQuery(recordType: "theUsers", predicate: pred)
publicDB.performQuery(myQuery, inZoneWithID: nil) {
results, error in
println("Results from publicDB Query: \(results)")
if error != nil {
println(error)
} else {
for record in results {
if var aselfie = record.objectForKey("selfie") as CKAsset! {
if var url = aselfie.fileURL {
var imageData = UIImage(contentsOfFile: url.path!)!
self.matchedSelfie.append(imageData)
}
println(self.matchedSelfie)
println("total selfies: \(self.matchedSelfie.count)")
var cellscount = self.matchedSelfie.count
}
}
}
println("Done Loading")
self.myCollectionView.reloadData()
}
}//View Did Load
func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView: Int) -> Int {
self.myCollectionView.reloadData()
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection: Int) -> Int {
self.myCollectionView.reloadData()
println("Selfies: \(self.matchedSelfie.count)")
println(matchedSelfie.count)
myCollectionView.reloadData()
return self.matchedSelfie.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as CollectionViewCell
println("just seeing when this prints")
cell.amatchedSelfie.image = image
return cell
}
}
答案 0 :(得分:0)
你知道它失败了吗?将图像分配给cell.amatchedSelfie.image时图像的价值是多少?在分配给cell.amatchedSelfie.image
之前,你可能会检查nil