集合视图控制器崩溃并且不返回图像

Collection View Controller crashing and not returning images

我正在为 CloudKit 数据库中的 return 图像构建一个集合视图。结果 returning 缓慢,但它们正在 returning。但是,我遇到了经典的 "fatal error: unexpectedly found nil while unwrapping an Optional value" 错误,我无法弄清楚为什么或必须修复什么才能解决该错误。有人对我如何解决这个问题有什么建议吗?

我的代码如下:

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
    }

}

你知道它失败的是哪一行吗?将图像分配给 cell.amatchedSelfie.image 时图像的值是多少?也许你可以在分配给 cell.amatchedSelfie.image

之前检查 nil