0

According to the question answered in here

A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.

Thus,

//First Attempt
var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]
        println(dict)

//output:
[name2: Roy, name1: Loy]

//Second Attempt
var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]
        println(dict)

//output:
[name2: Loy, name1: Roy]

My question is,

Is it guaranteed that if the content of the Dictionary is not changed the order will remain same?

For example for above example, First Attempt always returns the same order whatever the number of runs?

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • 1
    whether it does or does not, you should never rely on it. – Soroush Oct 17 '19 at 11:11
  • 1
    Do you mean “different runs of the program” or “printing the same dictionary repeatedly without modifying it in between”? – Martin R Oct 17 '19 at 11:22
  • If you need to keep the elements order you need an array of tuples (key,value) pairs – Leo Dabus Oct 17 '19 at 11:44
  • https://stackoverflow.com/questions/31527754/sort-dictionary-by-key-value/31528848 – Leo Dabus Oct 17 '19 at 11:45
  • Possible duplicate of https://stackoverflow.com/questions/48818931/how-to-access-dictionary-items-sequentially-in-swift-ios/48819801 – Leo Dabus Oct 17 '19 at 11:46
  • @MartinR yes “printing the same dictionary repeatedly without modifying it in between” but other code not related to dictionary of the program can be modified. – Sazzad Hissain Khan Oct 17 '19 at 11:57

2 Answers2

4

No, it is not guaranteed. No order means no order. Do not try to rely on undocumented accidents. In fact Apple explicitly warns that in modern Swift the order is random and could be different for different runs.

In your example, where you have only a literal, you could get a guaranteed order by casting to a KeyValuePairs.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Matt thanks. It would be good if we get reference of the comment “ In fact Apple explicitly warns that in modern Swift the order is random and could be different for different runs”. – Sazzad Hissain Khan Oct 17 '19 at 11:16
2

As long as you don't modify a dictionary, the order of it's key/value pairs does not change. Everything else is unspecified. From the documentation (emphasis added):

The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable.

Note: This applies only to printing/enumerating the dictionary within a single program run. As @matt said, the order is randomized on each program run. And this is because the Hasher is randomized:

Do not save or otherwise reuse hash values across executions of your program. Hasher is usually randomly seeded, which means it will return different values on every new execution of your program. The hash algorithm implemented by Hasher may itself change between any two versions of the standard library.

Hash randomization was enforced in Swift 4.2, with the implementation of SE 0206 Hashable Enhancements:

To implement nondeterminism, Hasher uses an internal seed value initialized by the runtime during process startup. The seed is usually produced by a random number generator, but this may be disabled by defining the SWIFT_DETERMINISTIC_HASHING environment variable with a value of 1 prior to starting a Swift process.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382