I mean: Is the order of keys and values in an NSDictionary always the same like how they were specified when initializing the NSDictionary? Or should I better maintain a seperate NSArray if I really need to know the order of keys?
6 Answers
No, they are not ordered. As long as you don't add or remove any elements from the dictionary, they will remain in the same order, but as soon as you add or remove an element, the new order will be completely different.
If you need the keys/values to be ordered, use an NSArray (or some other ordered data structure) instead.

- 390,455
- 97
- 512
- 589
-
2@kraag22: Nowhere in the `NSDictionary` documentation does it make any promises or guarantees about order -- it just says that it provides a "programmatic interface to objects that manage immutable associations of keys and values". The docs for the `-allKeys` and `-allValues` messages do call out that the order returned by those methods isn't defined, though it probably wouldn't be a bad idea to make that more explicit for other ways of enumerating the dictionary elements. – Adam Rosenfield Jan 16 '14 at 15:51
NSDictionary keys & values are not ordered. Your options:
- Maintain a separate array of the keys in the order you desire
- Write or find an existing class that is a wrapper for the above
- Write or find an existing subclass of NSDictionary that adds APIs for ordering

- 14,933
- 2
- 50
- 75
Matt Gallagher wrote a blog post titled “OrderedDictionary: Subclassing a Cocoa class cluster” covering exactly this issue, complete with sample code.

- 2,799
- 2
- 27
- 41
keys are never guaranteed to be in the same order when accessing an NSDictionary. If the keys can be compared (which I assume they can be given your question), then you can always sort them if you need to access them in sorted order.
You would need to do this by reading the keys into an array first and sorting the array of course.

- 124,830
- 17
- 198
- 235

- 46,381
- 14
- 112
- 137
NSDictionary, according to Apple's reference, is basically a wrapper around a hash table. So no, they are not guaranteed to be in any particular order.

- 8,455
- 3
- 24
- 25
-
How do they access that hash table? Have never seen a plain hash table, but would like to know how that looks like. Is that a big difference from using two synchronous NSArray internally? – Aug 18 '09 at 18:08
-
Are you asking what a hash table looks like as a data structure? Wiki has a pretty good explination: http://en.wikipedia.org/wiki/Hash_table If you want to know how Apple specifically implements and accesses them, I'm afraid the only things available to us are the header files ;) – Josh Lindsey Aug 18 '09 at 18:29
-
so when I get that right, a hash table is almost all about a hash function that will calculate the correct array-index for a given string-key + the array size. Sounds hard to implement, but good for performance. If that's true it would not matter much how many items are added to an dictionary. – Aug 19 '09 at 08:03
In fact, you can't even rely on the order being the same when running the program in two different devices, even if it's the exact same program and the exact same version of the operating system.
For example, if you run the program on an iPad Air, the ordering of the elements inside NSDictionary may be different than when running the same program on an iPad Retina, even if the iOS version is exactly the same.
In short, the ordering of elements in NSDictionary must never be relied on. You must always assume they may be in any unspecified order, which may be different on different devices.

- 363
- 2
- 9