0

I am a beginner in the swift programming language. I am having difficulty in understanding the order in which the labels in dictionaries are selected. For example in the code below :

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    print(kind)
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}

output: Fibonacci Square Prime

So here it is clear that the dictionary is neither following the order in which I entered the stuff nor the alphabetic order. So my question is in what order are this labels are selected? and is there a way to force a particular order?

Mandar Sadye
  • 689
  • 2
  • 9
  • 30
  • 1
    Short answer: The order is *unspecified.* Do not rely on or assume anything. – Martin R Aug 25 '17 at 12:14
  • thank you for the reply. Is there a similar structure in which I can store data like above and is predictable? – Mandar Sadye Aug 25 '17 at 12:19
  • Use an *array* of a custom struct. See https://stackoverflow.com/questions/30969688/ordered-dictionary-in-swift for more options. – Martin R Aug 25 '17 at 12:22

0 Answers0