Reputation: 33
I try this
let dict: [String:String] = ["1":"one","2":"two","3":"three","4":"four"]
let keyArray = [String](dict.keys)
let valueArray = [String](dict.values)
println(dict)
println(keyArray)
println(valueArray)
i hope the result should be
//dict = ["1":"one","2":"two","3":"three","4":"four"]
//keyArray = ["1","2","3","4"]
//valueArray = ["one","two","three","four"]
but it's not it is
//dict = ["4": "four", "2": "two", "1": "one", "3":"three"]
//keyArray = ["4","2","1","3"]
//valueArray = ["four", "two", "one", "three"]
How do I get my hope result
Thank for any help
Upvotes: 0
Views: 524
Reputation: 285064
From the Swift Language Guide
A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.
If you want a kind of ordering, get the keys, order them and get the associated values.
There are custom classes OrderedDictionary
on GitHub with a backing array for the keys to keep the order.
Upvotes: 3