简体   繁体   English

快速致命错误:解开Optional值时意外发现nil

[英]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. 每次运行它时, self.collectionView.dataSource = self行都会突出显示,并且出现上述错误。

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. 正如@ Vitaliy1所说,您可以将collectionView用作强引用,或在将其添加到视图层次结构之前使用局部变量对其进行打孔。

...

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 或者,为什么不只使用UICollectionViewController的子类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value Swift-致命错误:解开Optional值时意外发现nil - Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values Swift中的可选类型错误:致命错误:解开可选值时意外发现nil - Optional type error in Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview 致命错误:解开可选值(lldb)时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 致命错误:展开一个可选值(lldb)时意外发现nil - Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM