我如何获得NSDictionary / NSMutableDictionary的原始订单? [英] How can i get Original order of NSDictionary/NSMutableDictionary?

查看:101
本文介绍了我如何获得NSDictionary / NSMutableDictionary的原始订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有10个键的NSMutableDictionary。现在我想以与添加到NSMutableDictionary相同的顺序访问NSMutableDictionary键(使用SetValue:* forKey:*);

i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* );

我如何实现这一目标?

推荐答案

如果绝对必须使用字典容器,则必须使用密钥可以按添加键值对的顺序进行排序。因此,在创建字典时,使用自动递增整数或类似的键。然后,您可以对(整数)键进行排序,并检索与这些键关联的值。

If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and retrieve the values associated with those keys.

但是,如果你做了所有这些,你也可以只使用 NSMutableArray 并将值添加到数组直接!它会更快,需要更少的代码。您只需按顺序检索对象:

If you do all of that, however, you may as well just use an NSMutableArray and add values to the array directly! It will be much faster and require less code. You just retrieve objects in order:

for (id obj in myArray) { /* do stuff with obj... */ }

这篇关于我如何获得NSDictionary / NSMutableDictionary的原始订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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