Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've run into the same problem as found in this question. However, I have a follow-up question. I seem to be in the same situation as the original asker: I have a plist with a hierarchy of dictionaries that define a configuration screen. These are not mutable and will stay the same throughout the application. Since the original discussion seems to focus on problems arising from mutating the dictionary, I must ask for comfirmation: is the order of a dictionary guaranteed the same as they are in the plist, i.e. as it is read (with initWithContentsOfFile)? Can I use allKeys on it in this case to get a correct-order array of keys if the dictionary never changes?

like image 601
Thaurin Avatar asked Mar 22 '10 23:03

Thaurin


2 Answers

No, the keys are not likely to appear in the same order as your plist file. I can't look under the hood, but I would guess that the keys are ordered in whatever way that provides an efficient look-up. The documentation for allKeys says that the order of the elements in the array is not defined.

like image 173
dreamlax Avatar answered Nov 14 '22 22:11

dreamlax


If you need an NSDictionary that maintains order, take a look at CHDataStructure's CHOrderedDictionary, which does exactly that. I use it all the time.

like image 35
Dave DeLong Avatar answered Nov 14 '22 20:11

Dave DeLong