[英]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.