You can iterate through a dictionary and grab an index with input and enumeration (as others have said, there is no guarantee that it will exit as shown below)
let dict = ["c": 123, "d": 045, "a": 456] for (index, entry) in enumerate(dict) { println(index)
If you want to sort first ..
var sortedKeysArray = sorted(dict) { $0.0 < $1.0 } println(sortedKeysArray) // [(a, 456), (c, 123), (d, 45)] var sortedValuesArray = sorted(dict) { $0.1 < $1.1 } println(sortedValuesArray) // [(d, 45), (c, 123), (a, 456)]
then iteration.
for (index, entry) in enumerate(sortedKeysArray) { println(index)
If you want to create an ordered dictionary, you should look at Generics.
Matthew Korporaal Mar 31 '15 at 2:25 2015-03-31 02:25
source share