Swift 使用不同的键和值解析字典
我有一本这样的字典:Swift 使用不同的键和值解析字典,swift,dictionary,Swift,Dictionary,我有一本这样的字典: var mapNames: [String: [String]] = [ "A": ["A1", "A2"], "B": ["B1"], "C": ["C1", "C2", "C3"] ] 现在我有了第一视图控制器的tableview功能: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPat
var mapNames: [String: [String]] =
[
"A": ["A1", "A2"],
"B": ["B1"],
"C": ["C1", "C2", "C3"]
]
现在我有了第一视图控制器的tableview功能:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
//I want to print "A", "B", "C" here.
}
在我的第二视图控制器的tableview“func”中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//If user taps on "A" in the 1st VC, show here cells with "A1", "A2"
}
有人可以帮助我如何解析字典,首先获取键列表,然后为每个键获取对应值列表
谢谢。要解决此问题,请尝试将
字典
拆分为数组
例如:
var mapNames: [String: [String]] =
[
"A": ["A1", "A2"],
"B": ["B1"],
"C": ["C1", "C2", "C3"]
]
var mapNamesKeys = mapNames.keys
var mapNamesValues = mapNames.values
要解决此问题,请尝试将
字典
拆分为数组
例如:
var mapNames: [String: [String]] =
[
"A": ["A1", "A2"],
"B": ["B1"],
"C": ["C1", "C2", "C3"]
]
var mapNamesKeys = mapNames.keys
var mapNamesValues = mapNames.values
正如Eric所指出的,您应该先阅读文档。要获取字典的键值,需要执行以下操作:
for (key, value) in mapNames {
print(key)
print(mapNames[key])
}
正如Eric所指出的,您应该先阅读文档。要获取字典的键值,需要执行以下操作:
for (key, value) in mapNames {
print(key)
print(mapNames[key])
}
要从字典中获取所有键,请使用
let keys = mapNames.keys
//keys is the array you need on 1st view controller
现在,要获取每个键对应的数组,请使用
for key in keys
{
let value = mapNames[key]
//value is the array you need on 2nd view controller corresponding to the selected key
}
要从字典中获取所有键,请使用
let keys = mapNames.keys
//keys is the array you need on 1st view controller
现在,要获取每个键对应的数组,请使用
for key in keys
{
let value = mapNames[key]
//value is the array you need on 2nd view controller corresponding to the selected key
}
从源头上学习:从源头上学习: