我正在尝试使用 swift 在聊天应用程序上工作,当我在运行期间构建和运行项目时,当我尝试转到 ChatVewController 时,它会显示此 EXC 错误指令错误。
控制台说
致命错误:在展开可选值时意外发现 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.
谢谢!