performSegueWithIdentifier:在展开可选值时意外发现 nil
performSegueWithIdentifier: unexpectedly found nil while unwrapping an Optional value
我希望允许选择某一行,由此将发生如下概述的操作:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// Ensure controller knows which dataset to pull from,
// so detail view is correct
var friendChat: Friend!
friendChat = mappedFriends[indexPath.row]
// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if(friendChat.statusSort == 2) {
var controller : IndividualChatController!
print(friendChat.name)
controller.friendChat? = friendChat
controller.senderId? = Global.sharedInstance.userID
controller.senderDisplayName? = Global.sharedInstance.userName
self.performSegueWithIdentifier("showIndividualChat",sender: controller)
} else
if (friendChat.statusSort == 1) {
print("Can invite to be friend")
} else if (friendChat.statusSort == 0) {
print("Invite to Feast")
}
}
然而,在分配期间,:
controller.friendChat? = friendChat
controller.senderId? = FeastGlobal.sharedInstance.userID
controller.senderDisplayName? = FeastGlobal.sharedInstance.userName
出现错误:fatal error: unexpectedly found nil while unwrapping an Optional value
。
为什么会出现这种情况?如何缓解这种情况?
您在这里声明,但没有为变量 controller
赋值:
var controller : IndividualChatController!
您通过将类型设置为带有感叹号的隐式解包可选,即 IndividualChatController!
来承诺它不是 nil
但是,这个承诺没有被遵守,因为没有给它赋值,而且在这段代码运行时它实际上是 nil:
controller.friendChat? = friendChat
这就是您收到致命错误的原因。
解决方法是在声明时为 controller
变量赋值。例如:
var controller:IndividualChatController = IndividualChatController()
我希望允许选择某一行,由此将发生如下概述的操作:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// Ensure controller knows which dataset to pull from,
// so detail view is correct
var friendChat: Friend!
friendChat = mappedFriends[indexPath.row]
// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if(friendChat.statusSort == 2) {
var controller : IndividualChatController!
print(friendChat.name)
controller.friendChat? = friendChat
controller.senderId? = Global.sharedInstance.userID
controller.senderDisplayName? = Global.sharedInstance.userName
self.performSegueWithIdentifier("showIndividualChat",sender: controller)
} else
if (friendChat.statusSort == 1) {
print("Can invite to be friend")
} else if (friendChat.statusSort == 0) {
print("Invite to Feast")
}
}
然而,在分配期间,:
controller.friendChat? = friendChat
controller.senderId? = FeastGlobal.sharedInstance.userID
controller.senderDisplayName? = FeastGlobal.sharedInstance.userName
出现错误:fatal error: unexpectedly found nil while unwrapping an Optional value
。
为什么会出现这种情况?如何缓解这种情况?
您在这里声明,但没有为变量 controller
赋值:
var controller : IndividualChatController!
您通过将类型设置为带有感叹号的隐式解包可选,即 IndividualChatController!
但是,这个承诺没有被遵守,因为没有给它赋值,而且在这段代码运行时它实际上是 nil:
controller.friendChat? = friendChat
这就是您收到致命错误的原因。
解决方法是在声明时为 controller
变量赋值。例如:
var controller:IndividualChatController = IndividualChatController()