NSDictionary dictionaryWithContentsOfFile不会按顺序加载数据 [英] NSDictionary dictionaryWithContentsOfFile doesnt load the data in order

查看:152
本文介绍了NSDictionary dictionaryWithContentsOfFile不会按顺序加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将plist中的某些数据加载到数组中

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]]; 
}

但是它似乎不以与MyLevels.plist相同的顺序输出到数组中.

But it does not seem to output in the same order as the MyLevels.plist into the array.

MyLevels.plist看起来像这样: [键:1值:1] [键:2值:两个] [关键:3个值:三个] [关键:4值:四个] [键:5值:5]

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

知道为什么吗? 甚至还有另一种执行for循环的方式也可能是正确的顺序.

Any idea why?? Or even another way of doing the for loop maybe so it is in the correct order.

谢谢

推荐答案

NSDictionary对象不保留顺序.您不应该期望键的读取顺序与枚举顺序相同.

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

这篇关于NSDictionary dictionaryWithContentsOfFile不会按顺序加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆