我正在尝试使用 swift 开发聊天应用程序,当我在运行期间构建并运行该项目时,当我尝试转到 ChatVewController 时,它显示此 EXC 错误指令错误。
控制台显示
fatal error :解包可选值时意外发现 nil。
这是我的代码。
import UIKit
import Firebase
class SportsLogin: UIViewController {
// MARK: Properties
let ref: Firebase = Firebase(url: BASE_URL2)
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
let navVC2 = segue.destinationViewController as! UINavigationController
let chatVC2 = navVC2.viewControllers.first as! SportsViewController
chatVC2.senderId = ref.authData.uid // Assign the local user’s ID to chatVc.senderId; this is the local ID that JSQMessagesViewController uses to coordinate messages.
chatVC2.senderDisplayName = "" // chatVc.senderDisplayName is set to empty string, since this is an anonymous chat room.
}
@IBAction func loginDidTouch(sender: AnyObject) {
ref.authAnonymouslyWithCompletionBlock { (error, authData) -> Void in
if error != nil { // Check for an authentication error.
print(error.description)
return
}
// Inside of the closure, trigger the segue to move to ChatViewController.
print(self.ref.authData)
self.performSegueWithIdentifier("SportChat", sender: self)
}
}
}
有错误的行是
chatVC2.senderId = ref.authData.uid // Assign the local user’s ID to chatVc.senderId; this is the local ID that JSQMessagesViewController uses to coordinate messages.
谢谢!
最佳答案
SportsViewController 是什么?如果SportViewController是自己delcare的话,变量成员应该是sendId,是class memeber,NSString类型。
关于swift - fatal error - 解包可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854846/