Firebase访问Swift 3中的快照值错误 [英] Firebase Accessing Snapshot Value Error in Swift 3
问题描述
我最近升级到swift 3,并且在尝试从快照观察事件值访问某些事物时遇到错误。
我的代码:
$ b $ pre $ ref $ c $ ref ref.child(users)。child(userID!)。observeSingleEvent(of:.value,with:{(snapshot)in
let username = snapshot.value![fullName] as!String
let homeAddress = snapshot.value![homeAddress] as![Double]
let email = snapshot.value![email] as!String
}
上面列出的三个变量和状态:
类型'任何'没有下标成员
任何帮助将是非常感谢
我认为你可能需要投射你的 snapshot.value
作为 NSDictionary
。
ref.child(users ).child(userID!)。observeSingleEvent(:.value,其中:{(snapshot)in
let value = snapshot .value as?NSDictionary
let username = value?[fullName] as?字符串?
让homeAddress = value?[homeAddress]为? [双]? []
让email = value?[email]为?字符串?
}
您可以参阅firebase文档: a href =https://firebase.google.com/docs/database/ios/read-and-write =noreferrer> https://firebase.google.com/docs/database/ios/read-并写入
I recently upgraded to swift 3 and have been getting an error when trying to access certain things from a snapshot observe event value.
My code:
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let username = snapshot.value!["fullName"] as! String
let homeAddress = snapshot.value!["homeAddress"] as! [Double]
let email = snapshot.value!["email"] as! String
}
The error is around the three variables stated above and states:
Type 'Any' has no subscript members
Any help would be much appreciated
I think that you probably need to cast your snapshot.value
as a NSDictionary
.
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let username = value?["fullName"] as? String ?? ""
let homeAddress = value?["homeAddress"] as? [Double] ?? []
let email = value?["email"] as? String ?? ""
}
You can take a look on firebase documentation: https://firebase.google.com/docs/database/ios/read-and-write
这篇关于Firebase访问Swift 3中的快照值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!