简体   繁体   中英

swift fatal error: unexpectedly found nil while unwrapping an Optional value

I'm new to swift and I can't quite figure out how to address this error.

I'm creating a collection view and this is my code:

import UIKit

class FlashViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!

        override func viewDidLoad() {
        super.viewDidLoad()
        // Move on ...
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 90)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
        cell.imageView?.image = UIImage(named: "circle")
        return cell
    }
}

every time I run it the self.collectionView.dataSource = self line gets highlighted and I get the above mentioned error.

As you use weak reference your collection view released before you call it.

So you have to make it strong by removing "weak" keyword.

@IBOutlet var collectionView: UICollectionView!

Or make it stay in memory in another way.

...

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

// because collectionView is a weak variable, it will be released here
self.collectionView.dataSource = self // error, collectionView is nil

...

As @Vitaliy1 said, you can make collectionView a strong reference, or use a local variable to hole it before you add it to the view hierarchy.

...

let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self

...

view.addSubview(collectionView)
// view establishes a strong reference to collectionView,
// so you can reference it until it is removed from the view hierarchy.
self.collectionView = collectionView

or, why not just use a subclass of UICollectionViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM