【发布时间】:2017-02-02 17:05:46
【问题描述】:
我正在学习以下课程。
class FriendsProfileViewController: UIViewController {
var user : User!
override func viewDidLoad() {
super.viewDidLoad()
print(user.firstName)
}
}
在 segueing 类中,我做了以下操作来传递数据
var friends: [User]?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("viewFriend", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController
if let friendsProfileVC = destinationVC as? FriendsProfileViewController{
if let indexPath = sender as? NSIndexPath{
print(indexPath.row)
friendsProfileVC.user = friends?[indexPath.row]
}
}
}
为什么我收到错误
致命错误:在展开可选值时意外发现 nil
即使我的friend?[indexPath.row] 在我转场时被验证包含一个值?。
【问题讨论】:
-
你调试了吗?如果是,发生了什么?
-
在哪一行你正面临给定代码的崩溃??并且您的已初始化或为空??
-
您是否获得了
print(indexPath.row)行的控制台输出? -
这意味着您正在传递用户的 nil 值。你应该取 User 的可选值,或者你应该确保你没有传递 nil 值。
-
检查
friends是否为nil