开发者

NSDictionary dictionaryWithContentsOfFile doesnt load the data in order

开发者 https://www.devze.com 2023-02-11 05:06 出处:网络
I am trying to load some data fr开发者_如何学Pythonom a plist into an array NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile:[ [ [ NSBundlemainBundle] bundlePath ] stringByApp

I am trying to load some data fr开发者_如何学Pythonom 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


NSDictionary objects do not preserve ordering. You should not expect to have keys enumerated in the same order in which they were read.


If order is important, you should create an array in your plist, and access it from one of the dictionary keys.


Any idea why?

Yes. NSDictionary is unordered.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号