致命错误 - 在展开可选值时意外发现nil

时间:2016-04-26 03:38:33

标签: swift exc-bad-instruction

我正在尝试使用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.

谢谢!

1 个答案:

答案 0 :(得分:0)

什么是SportsViewController?如果SportViewController是你自己的delcare,你应该有变量的成员是sendId,是类memeber和NSString类型。