gpt4 book ai didi

swift - 尝试获取 Firebase 数据时获取 "nil while unwrapping an Optional value"

转载 作者:行者123 更新时间:2023-11-30 12:44:16 25 4
gpt4 key购买 nike

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

            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 :在解包可选值时意外发现 nil
2017-01-24 15:48:23.845240 A[10696:2902570] fatal error :在解包可选值时意外发现 nil

堆栈:

    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

最佳答案

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

})

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

关于swift - 尝试获取 Firebase 数据时获取 "nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com