NSDictionary dictionaryWithContentsOfFile doesnt load the data in order

╄→尐↘猪︶ㄣ 提交于 2020-01-06 08:39:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!