NSDictionary的allKeys数组搞砸了 - 不是按照它们在字典中的顺序? [英] NSDictionary's allKeys array messed up - not in the order they are in the dictionary?

查看:523
本文介绍了NSDictionary的allKeys数组搞砸了 - 不是按照它们在字典中的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我从.plist中提取数据,更具体地说是从NSDictionary中提取数据。我使用.plist的内容初始化一个字典,效果很好。
当我这样做时

in my project I'm pulling data from a .plist, more concretely from an NSDictionary. I init a dictionary with the contents of the .plist, which works well. When I then do

NSArray *array = [dict allKeys];

它用所有键填充数组,但是以完全随机的顺序,与它们的顺序不同在.plist文件中。我真的需要保留订单。
字典中的键是数组,如果这可能导致问题。
我没有得到什么?

it fills the array with all the keys, but in a totally random order, different to the order they are in the .plist file. I would really need to preserve the order. The keys in the dictionary are arrays, if that could cause a problem. What am I not getting?

提前多多谢谢!

推荐答案

就像NSSet中的项目之间没有顺序一样,NSDictionary中的键值对没有固有的顺序。

Much like there is no order between items in an NSSet, there's is no inherent order to the key-value pairs within an NSDictionary.

如果你想要特定订单下的密钥,您需要使用类似 - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr 但不会给出在plist文件中序列化NSDictionary的顺序。

If you want keys under a specific order, you'd need to use something like - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr but that won't give the order in which the NSDictionary was serialized in the plist file.

这篇关于NSDictionary的allKeys数组搞砸了 - 不是按照它们在字典中的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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