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.