var dictionary: Dictionary<String, AnyObject> = ["title" : "Berlin",
"description" : "Berlin square",
"createdBy": "Mathias",
"coordinates": ["fddfhsg": ["latitude": 56.34, "longitude": 53.44]]]
dictionary["coordinates"]
我需要获取纬度和经度值,但我不键入(“fddfhsg”)字典。此密钥由Firebase数据库生成。
感谢您的帮助。
答案 0 :(得分:6)
您可以在不知道此密钥的情况下获取这些值:cast&#34; coordinates&#34;到正确的字典类型然后使用可选绑定和values
属性。
示例:
if let dict = dictionary["coordinates"] as? [String:[String:Double]],
content = dict.values.first,
latitude = content["latitude"],
longitude = content["longitude"] {
print(latitude)
print(longitude)
}
56.34
53.44
作为评论之后的更新:您现在需要做的就是适应不同的字典结构。
对于您的第二本词典,请查看其结构:
for (key, value) in dictionary2 {
print(key)
print(value)
print("---")
}
您要查找的值与我们之前获取的其他值不同。
调整我已经给出的解决方案:它只是字典中的层次结构问题。在dictionary1
中,未知密钥位于&#34;坐标&#34;中,但这个位于根级别。