在展开可选值时获取" nil"在尝试获取Firebase数据时

时间:2017-01-24 23:49:50

标签: swift firebase swift3 firebase-realtime-database

第一次打印后,我的应用程序崩溃了

            print("got here")
            ref.child("Used").observeSingleEvent(of: .value, with: { (snapshot) in
                print("got here?")
                // do something
            })

但是,在脚本

中执行相同的调用时,它不会崩溃
ref.child("Used").observeSingleEvent(of: .value, with: { (snapshot) in

        // Check if exists
        if snapshot.hasChild(self.TextField.text!) {
            let joinInfo = ["info"     : self.TextField.text!]
            self.Information = joinInfo
            self.performSegue(withIdentifier: "Segue", sender: self)
        } else {
            // TODO Alert error
        }
    })

第一个代码块的完整代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Segue" {
            if let destination = segue.destination as? ViewController {                                        
                // Create a dispatch group to sync all Firebase call
                print("got here ")
                ref.child("Used").observeSingleEvent(of: .value, with: { (snapshot) in
                    print("got here?")

                })                  
            }
    }
}

我收到的错误消息是:fatal error: unexpectedly found nil while unwrapping an Optional value 2017-01-24 15:48:23.845240 A[10696:2902570] fatal error: unexpectedly found nil while unwrapping an Optional value

堆栈:

    0x10081d1e4 <+96>:  nop    
0x10081d1e8 <+100>: mov    x0, x25
0x10081d1ec <+104>: mov    x1, x24
0x10081d1f0 <+108>: mov    x2, x23
0x10081d1f4 <+112>: mov    x4, x8
0x10081d1f8 <+116>: bl     0x100710b80               ; function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
->  0x10081d1fc <+120>: brk    #0x1

1 个答案:

答案 0 :(得分:0)

ref.child("Used").observeSingleEvent(of: .value, with: { (snapshot) in
                    print("got here?")

            }) 

进入之前的ref.电话。将joinInfo从let更改为var。