Arrays Swift 3-从字典数组中获取值

Arrays Swift 3-从字典数组中获取值,arrays,dictionary,swift3,Arrays,Dictionary,Swift3,尝试获取存储在数组中的字典的值和键以填充两个UILabel 我的数组如下所示: var fromLocations = [["marco" : "polo"],["jekyll" : "hide"],["freddy" : "jason"]] 我通过集合视图的索引XPath获取索引。目前正在尝试以下操作: cell.locationName.text = fromLocations[indexPath.item].values[1] 我试过很多其他的方法,但我做不到 这就是你想要做的吗 va

尝试获取存储在数组中的字典的值和键以填充两个UILabel

我的数组如下所示:

var fromLocations = [["marco" : "polo"],["jekyll" : "hide"],["freddy" : "jason"]]
我通过集合视图的索引XPath获取索引。目前正在尝试以下操作:

cell.locationName.text = fromLocations[indexPath.item].values[1]

我试过很多其他的方法,但我做不到

这就是你想要做的吗

var fromLocations = [["marco" : "polo1"],["marco" : "polo2"],["marco" : "polo3"]]
let marco = fromLocations[0]["marco"]
print("marco = \(marco)")
这印的是“polo1”。基本上,您正在访问数组的第一项,即字典。然后,您可以按照通常访问词典的方式访问该词典

因此,细分如下:

let fromLocations = [["marco" : "polo"],["marco" : "polo"],["marco" : "polo"]]
let theDictionary = fromLocations[0]
let value = theDictionary["marco"]

要使用键(这很奇怪)以数组形式获取值,请将值转换为数组

var fromLocations = [["marco" : "polo1"],["marco" : "polo2"],["marco" : "polo3"]]
let marco = Array(fromLocations[0].values)[0]
print("marco = \(marco)")

这将打印“polo1”

某种程度上,我已经使用占位符文本来显示一系列字典-马可和波罗在每一组中都是不同的。啊,明白了。如果访问不需要字典键,为什么不使用字符串数组呢?谢谢-我太聪明了,我想字符串数组就可以了:)