问题
I am trying to load some data from a plist into an array
NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile:[ [ [ NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"MyLevels.plist" ] ];
NSMutableArray *panelNames1;
panelNames1 = [[NSMutableArray alloc] init];
for (id theKey in mydictionary) {
[panelNames1 addObject:[mydictionary objectForKey:theKey]];
}
But it does not seem to output in the same order as the MyLevels.plist into the array.
MyLevels.plist looks likes this: [Key:1 Value: One] [Key:2 Value: Two] [Key:3 Value: Three] [Key:4 Value: Four] [Key:5 Value: Five]
But it reads it in this order: Three, One, Four, Two, Five
Any idea why?? Or even another way of doing the for loop maybe so it is in the correct order.
Thanks
回答1:
NSDictionary
objects do not preserve ordering. You should not expect to have keys enumerated in the same order in which they were read.
回答2:
If order is important, you should create an array in your plist, and access it from one of the dictionary keys.
回答3:
Any idea why?
Yes. NSDictionary
is unordered.
来源:https://stackoverflow.com/questions/5010539/nsdictionary-dictionarywithcontentsoffile-doesnt-load-the-data-in-order