我希望能够在firebase中获取用户的地址和名称,但我找不到办法。 这是我最大的努力。这在Xcode中看起来很好,但是当我按下按钮来检索值时,应用程序崩溃了(线程1 exc_bad_instruction(code = exc_i386_invop subcode = 0x0)
ref?.child("users").child(user)
.observeSingleEvent(of: .value, with: { (snapshot) in
let userDict = snapshot.value as! [String: Any]
let address = userDict["Address:"] as! String
let name = userDict["Name:"] as! String
print("Address: \(address) Name: \(name)")})
答案 0 :(得分:2)
nil
)并且如果你的一个值变成nil
,那么将崩溃的应用程序。试试这样的事情 -
ref?.child("users").child(user).observeSingleEvent(of: .value, with: { snapshot in
guard let userDict = snapshot.value as? [String: Any],
let address = userDict["Address:"] as? String,
let name = userDict["Name:"] as? String else {
return
}
print("Address: \(address) Name: \(name)")
})
如果您Name
或Address
中的任何一个nil