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

时间:2017-06-19 14:48:30

标签: arrays dictionary swift3

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

我的数组如下所示:

var fromLocations = [["marco" : "polo"],["jekyll" : "hide"],["freddy" : "jason"]]

我通过集合视图的indexPath获取索引。目前正在尝试以下方法:

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

我尝试过其他一些方法,但我无法指出。

1 个答案:

答案 0 :(得分:2)

这是你想要做的吗?

import bz2

with bz2.BZ2File('db.json.bz2', 'w', buffering=1024) as f:
  django.core.management.call_command('dumpdata', stdout=f)

这会打印“polo1”。基本上你正在访问数组的第一项是字典。然后,您可以像通常访问字典一样访问该字典。

所以分解是:

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

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

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

这将打印“polo1”

相关问题