NSDictionary DictionaryWithContentsOfFile 未按顺序加载数据
我正在尝试将一些数据从 plist 加载到数组中,
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 相同的顺序输出到数组中。
MyLevels.plist 看起来像这样: [键:1 值:1] [键:2 值:二] [键:3 值:三] [键:4 值:4] [键:5 值:五]
但它按以下顺序读取:三、一、四、二、五
知道为什么吗? 或者甚至是执行 for 循环的另一种方法,因此它的顺序是正确的。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDictionary
对象不保留顺序。您不应该期望键的枚举顺序与读取键的顺序相同。NSDictionary
objects do not preserve ordering. You should not expect to have keys enumerated in the same order in which they were read.如果顺序很重要,您应该在 plist 中创建一个数组,并从字典键之一访问它。
If order is important, you should create an array in your plist, and access it from one of the dictionary keys.
是的。
NSDictionary
是无序的。Yes.
NSDictionary
is unordered.