在SWIFT 3.0中访问嵌套的NSDictional值 [英] Accessing Nested NSDictionary values in Swift 3.0
本文介绍了在SWIFT 3.0中访问嵌套的NSDictional值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从Firebase收到了以下数据。我已将我的SnaphotValue设置为NSD词典。
self.ref.child("users").child(facebookID_Firebase as! String).observeSingleEvent(of: .value, with: { (snapshot) in
let snapshotValue = snapshot.value as? NSDictionary
print(snapshotValue, "snapshotValue")
//this line of code doesn't work
//self.pictureURL = snapshot["picture"]["data"]["url"]
}) { (error) in
print(error.localizedDescription)
}
我尝试了How do I manipulate nested dictionaries in Swift, e.g. JSON data?、How to access deeply nested dictionaries in Swift和其他解决方案,但没有成功。
如何访问数据键和图片键内的url值?
我可以在Firebase中进行另一个引用并获得值,但我正在尝试保存另一个请求。😓
推荐答案
当您在Swift中引用字典的键时,您会得到一个展开的值。这意味着它可以为零。您可以强制解开该值,也可以使用Prettyif let =
这可能会起作用。
if let pictureUrl = snapshot["picture"]["data"]["url"] {
self.pictureURL = pictureUrl
}
这篇关于在SWIFT 3.0中访问嵌套的NSDictional值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文