如何获得无字典密码的价值? [英] How to obtain values without key of dictionary?
问题描述
var dictionary: Dictionary<String, AnyObject> = ["title" : "Berlin",
"description" : "Berlin square",
"createdBy": "Mathias",
"coordinates": ["fddfhsg": ["latitude": 56.34, "longitude": 53.44]]]
坐标]
我需要获取纬度和经度值,但我不键入(fddfhsg)字典。此键由Firebase数据库生成。
I need to obtain values latitude and longitude, but I don't key ("fddfhsg") of dictionary. This key is generates by Firebase database.
感谢您的帮助。
推荐答案
p>您可以在不知道此关键字的情况下获取这些值:将坐标转换为正确的字典类型,然后使用可选绑定和值
属性。
You can get these values without knowing this key: cast "coordinates" to the right dictionary type then use optional binding and the values
property.
示例:
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
56.34
53.44
更新您的意见后:您现在需要做的是适应不同的字典结构。
As an update after your comments: all you need to do now is to adapt to different dictionary structures.
对于您的第二本字典,请查看其结构:
For your second dictionary, look how it is structured:
for (key, value) in dictionary2 {
print(key)
print(value)
print("---")
}
您要查找的值不在与以前我们抓到的其他人相同的水平。
The values you're looking for are not at the same level than the other ones we grabbed previously.
调整我已经给出的解决方案:这只是字典中层次结构的问题。在 dictionary1
中,未知密钥位于坐标中,但这个密钥位于根级别。
Adapt the solution I've already given: it's only a matter of hierarchy in the dictionary. In dictionary1
the unknown key was in "coordinates", but this one is at the root level.
这篇关于如何获得无字典密码的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!