iOS Swift: 在 UICollectionView 中展开可选的致命错误
iOS Swift: Fatal error with unwrapping optional in UICollectionView
我正在尝试创建自定义集合视图。这是我第一次使用这种视图类型,也是我在中断 iOS 编程几年后第一次使用 Swift。
这是单元格的代码:
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
}
我收到以下错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
在线:
cell.name.text = p.name
我真的不知道为什么会这样。
编辑:
此处要求的是完整的 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
}
}
在 viewWillAppear 中,我有一个 println() 语句,它输出数组中每个条目的名称值,并且所有 6 个都出现了,所以我知道数组在那里不是 nil 值。另外,我尝试更改
cell.name.text = p.name
只是
cell.name.text = "test string"
看看这是否有所不同,但我仍然遇到同样的错误。
问题可能与以下情况有关。
var p: Administrator = principals[indexPath.row]
如果 Administrator 是 class 并且它有一个名为 属性 的名称,如果此 属性 是可选类型,不幸的是如果它没有值,则可能会产生问题.因此,如果是这种情况,请执行以下操作,
if let iName = p.name as? String {
cell.name.text = iName
}
管理员 class/struct 可以定义为
class Administrator {
var name: String! //or var name: String?
}
您需要删除 viewDidLoad()
中的以下行:
self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
如果您使用的是故事板,则不要调用它。它将覆盖您在情节提要中的内容。
我正在尝试创建自定义集合视图。这是我第一次使用这种视图类型,也是我在中断 iOS 编程几年后第一次使用 Swift。
这是单元格的代码:
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
}
我收到以下错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
在线:
cell.name.text = p.name
我真的不知道为什么会这样。
编辑:
此处要求的是完整的 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
}
}
在 viewWillAppear 中,我有一个 println() 语句,它输出数组中每个条目的名称值,并且所有 6 个都出现了,所以我知道数组在那里不是 nil 值。另外,我尝试更改
cell.name.text = p.name
只是
cell.name.text = "test string"
看看这是否有所不同,但我仍然遇到同样的错误。
问题可能与以下情况有关。
var p: Administrator = principals[indexPath.row]
如果 Administrator 是 class 并且它有一个名为 属性 的名称,如果此 属性 是可选类型,不幸的是如果它没有值,则可能会产生问题.因此,如果是这种情况,请执行以下操作,
if let iName = p.name as? String {
cell.name.text = iName
}
管理员 class/struct 可以定义为
class Administrator {
var name: String! //or var name: String?
}
您需要删除 viewDidLoad()
中的以下行:
self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
如果您使用的是故事板,则不要调用它。它将覆盖您在情节提要中的内容。