user1452936 user1452936 - 2 months ago 4
Swift Question

parse dictionary with different keys and values

I have a dictionary like this:

var mapNames: [String: [String]] =
[
"A": ["A1", "A2"],
"B": ["B1"],
"C": ["C1", "C2", "C3"]
]


Now I have a 1st View controller's tableview's func:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {

//I want to print "A", "B", "C" here.

}


In my 2nd View Controller's 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"

}


Can someone help with how can i parse my dictionary to get list of keys first and then for each key, get list of corresponding values ?

Thanks.

Answer

As pointed by Eric, you should read the documentation first. To get key - value of a dictionary, you need to do this:

for (key, value) in mapNames {
   print(key)
   print(mapNames[key])
}
Comments