[英]iOS Swift: Fatal error with unwrapping optional in UICollectionView
I am trying to create a custom collection view. 我正在尝试创建一个自定义集合视图。 This is my first time using this view type and also my first time using Swift after a few years hiatus from iOS programming. 这是我第一次使用这种视图类型,也是我从iOS编程中脱离几年后第一次使用Swift。
Here is the code for the cell: 这是单元格的代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PrincipalViewCell
// Configure the cell
var p: Administrator = principals[indexPath.row]
cell.name.text = p.name
return cell
}
I get the following error: 我收到以下错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
on line: 在线:
cell.name.text = p.name
I really am at a loss as to why this is happening. 我真的不知道为什么会这样。
Edit: 编辑:
As requested here is the full CollectionViewController: 根据要求,这里是完整的CollectionViewController:
import UIKit
let reuseIdentifier = "PrincipalCell"
var principals = [Administrator]()
class PrincipalViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
// self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
principals = appDelegate.admins
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return principals.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PrincipalViewCell
// Configure the cell
var p: Administrator = principals[indexPath.row]
cell.name.text = p.name
return cell
}
}
in the viewWillAppear I had a println() statement that output the name value for each entry in the array and all 6 appeared, so I know the array was not a nil value there. 在viewWillAppear中,我有一个println()语句,该语句输出数组中每个条目的名称值,并且所有6个字符都出现了,所以我知道数组中的值不是nil。 Also, I tried changing 另外,我尝试改变
cell.name.text = p.name
to just 只是
cell.name.text = "test string"
to see if that made a difference and I still got the same error. 看看是否有所作为,但我仍然遇到相同的错误。
The problem may be as per this below scenario. 问题可能是根据以下这种情况。
var p: Administrator = principals[indexPath.row]
If Administrator is a class and it has a property called name, if this property is of optional type and unfortunately if it does not have value it may create the problem. 如果Administrator是一个类,并且具有名为name的属性,则此属性为可选类型,并且不幸的是,如果该属性没有值,则可能会引起问题。 So if this is the case then do as below, 因此,如果是这种情况,请执行以下操作,
if let iName = p.name as? String {
cell.name.text = iName
}
The Administrator class/struct may b defined as 管理员类/结构可以b定义为
class Administrator {
var name: String! //or var name: String?
}
You need to remove the following line in your viewDidLoad()
: 您需要在viewDidLoad()
删除以下行:
self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
If you are using a storyboard you don't want to call this. 如果您使用的是情节提要,则不希望调用此脚本。 It will overwrite what you have in your storyboard. 它将覆盖情节提要中的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.