【问题标题】:Getting "nil while unwrapping an Optional value" when trying to get Firebase data尝试获取 Firebase 数据时获取“在展开可选值时为零”
【发布时间】:2017-01-24 23:49:49
【问题描述】:

我的应用在第一次打印后立即崩溃

            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

【问题讨论】:

    标签: swift firebase swift3 firebase-realtime-database


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多