swift - fatal error - 解包可选值时意外发现 nil

标签 swift exc-bad-instruction

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

相关文章:

ios - 在不支持并发的函数中调用'async' swift ios Xcode async/await

ios - 在键盘上打字时,点击按钮无法被识别

swift - Xcode Swift 快速帮助闭包

ios - 将数据字符串转换为 UTF8 时出现问题

ios - 一直可靠工作的时间戳函数只是导致了 EXC_BAD_INSTRUCTION

ios - 带有 SWRevealViewController 的 Exc_bad_instruction

ios - EXC_BAD_INSTRUCTION 当我在 Swift 2.0 中设置 UILabel 标题时

swift - 闭包中的弱 self 和后果示例

swift - 试图理解我得到的这个随机错误//swift

ios - SpriteKit 展开可选错误(Swift)