【问题标题】:swift fatal error: unexpectedly found nil while unwrapping an Optional value快速致命错误:在展开可选值时意外发现 nil
【发布时间】:2014-11-14 07:12:57
【问题描述】:

我是 swift 新手,我不太清楚如何解决这个错误。

我正在创建一个集合视图,这是我的代码:

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
    }
}

每次我运行它时,self.collectionView.dataSource = self 行都会突出显示,并且出现上述错误。

【问题讨论】:

  • 看来self.collectionViewnil。确保您的 IB 网点设置正确。您使用的是故事板还是 .xib 文件?
  • IBOutlet 属性应该从 Interface Builder 中设置,而很少从代码中设置,因此请确保您要在 viewDidLoad 中进行设置。

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

当您使用弱引用时,您的集合视图会在调用之前发布。

因此,您必须通过删除“弱”关键字来使其变得强大。

@IBOutlet var collectionView: UICollectionView!

或者以另一种方式让它留在记忆中。

【讨论】:

  • 这对我也有用。我正在关注一个教程并被困在这部分。我想知道有什么区别,因为它适用于示例项目...
【解决方案2】:
...

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

...

正如@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

或者,为什么不直接使用UICollectionViewController的子类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多