1
let dicForSections = ["Home":["Rooms","Stuff"],"My Profile": ["Property", "Agent", "Policy", "Claims"], "Help":["Recovery","ok"]]

I am using UITableView with multiple sections. I have an array with key values so I want to get their key in an array and their values in another array remember every value is a separate array because one array (key) for section title and other for each section rows.

2
  • 3
    This is a Dictionary<String:Array<String>> not a nested array. It's not clear what your asking and there's no "sample code below" Oct 3, 2014 at 6:58
  • Just want to save keys in a array and their values in another array.
    – Ali Raza
    Oct 3, 2014 at 7:03

3 Answers 3

4

Here is an example:

let dicForSections = ["Home":["Rooms","Stuff"],"My Profile": ["Property", "Agent", "Policy", "Claims"], "Help":["Recovery","ok"]]

var keys = [String]()
var values = [[String]]()

for (key, value) in dicForSections {
    keys.append(key)
    values.append(value)
}
3

Swift's Dictionary has computed properties that return both keys and values:

let dicForSections = ["Home":["Rooms","Stuff"],"My Profile": ["Property", "Agent", "Policy", "Claims"], "Help":["Recovery","ok"]]

var keys = [String](dicForSections.keys)
var values = [[String]](dicForSections.values)

Martin R suggests to let the compiler infer the type:

var keys = Array(dicForSections.keys)
var values = Array(dicForSections.values)
4
  • Alternatively keys = Array(dicForSections.keys) etc. The type is inferred automatically.
    – Martin R
    Oct 3, 2014 at 8:03
  • @MartinR That's cleaner, I concur. Thanks! Oct 3, 2014 at 8:04
  • The documentation about keys/values states that is returns an "unordered iterable collection of all of a dictionary’s keys/values". It does not explicitly guarantee that keys and values are returned in the same (corresponding) order (at least I could not find such a statement).
    – Martin R
    Oct 3, 2014 at 8:13
  • @MartinR I was considering to put this information in my answer. I did not because the OP did not specifically ask for parallel arrays and I thought it would just bloat the post. Oct 3, 2014 at 8:19
0

The other posters have all answered your question literally. Given your proposed usage and the fact that a dictionary is an unordered collection and that you usually want more control over the layout of a table view, I'd strongly suggest you reconsider your data format.

If you think about the data behind a table, it's really an Array of sections, each section has a title and some associated data.

This suggests to me a first cut reorganization of just using an Array of tuples:

let sections = [("Home", ["Rooms","Stuff"]), ("My Profile", ["Property", "Agent", "Policy", "Claims"]), ("Help",["Recovery","ok"])]

func nameForSection(section:Int) -> String {
    return sections[section].0 
}

func titleForIndexPath(index:NSIndexPath) -> String {
    return sections[index.section].1[index.row] 
}

This has the advantage that creating the data is extremely succinct, but neither the data creation or the code to access the data are particularly legible. As a side note, your existing data can be reorganized into this structure with this snippet, although ordering of the sections will be lost.

let sections = Array(dicForSections)

In Objective-C we might solve this with an array of dictionaries, but since you asked about Swift and Swift has embedded structures, let's just use those, and add some formatting while we're at it:

struct Section {
    let title:String
    let rows:[String]
}

let sections = [
    Section(title:"Home", rows:["Rooms","Stuff"]),
    Section(title:"My Profile", rows:["Property", "Agent", "Policy", "Claims"]),
    Section(title:"Help", rows:["Recovery","ok"])
]

func nameForSection(section:Int) -> String {
    return sections[section].title
}

func titleForIndexPath(index:NSIndexPath) -> String {
    return sections[index.section].rows[index.row]
}

Now both the layout of the data and the access to the data is much safer and legible, and the ordering of our sections is maintained since we haven't funneled it through a dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.