Iterating over a dictionary in Swift
Forums:
let dic = [1 : "one", 2 : "two"]
var str = ""
for (key, value) in dic {
str += (str == "" ? "[" : ", ") +
"\(key) : \"\(value)\""
}
str += "]"
//str is "[2 : "two", 1 : "one"]"
str = ""
for value in dic.values {
str += (str == "" ? "" : ", ") + value
}
//str is "two, one"
- Log in to post comments