Is the NSDictionary key order guaranteed to be the same as initialized if it never changes?

I ran into the same problem that I found in this question . However, I have the following question. I seem to be in the same situation as the original questionnaire: I have a plist with a hierarchy of dictionaries that define the configuration screen. They do not change and remain unchanged throughout the application. Since the initial discussion seems to focus on issues related to changing the dictionary, I have to ask for confirmation: the dictionary order is guaranteed in the same way as in plist, i.e. How is it read (with initWithContentsOfFile)? Can I use allKeys in this case to get the correct key array if the dictionary never changes?

+5
source share
4 answers

No, keys are unlikely to appear in the same order as your file plist. I cannot look under the hood, but I would suggest that the keys are arranged in such a way as to provide an efficient search. The documentation for allKeysindicates that the order of elements in the array is undefined.

+7
source

If you need NSDictionaryone that maintains order, check out CHDataStructureCHOrderedDictionary , which does just that. I use it all the time.

+6
source

, , allKeys .

, NSDictionary -.

allKeys :

The order of elements in the array is undefined.

If you want to display the values ​​in some order, you will get the keys, sort the array of keys and then get the value for each key.

+3
source

Nothing is guaranteed. It was not even guaranteed that iterating through the dictionary twice would give you the keys in the same order.

+2
source

All Articles