我如何获得 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:* );
How can i achieve that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您绝对必须使用字典容器,则必须使用可按添加键值对的顺序排序的键。因此,在创建字典时,您使用的键是自动递增整数或类似的键。然后,您可以对(整数)键进行排序并检索与这些键关联的值。
但是,如果您执行了所有这些操作,您也可以只使用 NSMutableArray 并直接向数组添加值!它将更快并且需要更少的代码。您只需按顺序检索对象:
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.
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:NSMutableDictionary
无法做到这一点。看看例如 Matt GallaghersOrderedDictionary
。NSMutableDictionary
can't do that. Take a look at e.g. Matt GallaghersOrderedDictionary
.我编写了一个快速方法来获取源数组(全部无序的对象)和引用数组(具有所需(且完全任意)顺序的对象),并返回一个数组,其中源数组的项目已重新组织以匹配参考数组。
请注意,这是非常脆弱的。它使用
NSArray
的containsObject:
方法,该方法最终将调用NSObject
的isEqual:< /代码>
。基本上,它应该适用于
NSString
、NSNumber
数组,也许还有NSDate
(还没有尝试过),但除此之外,YMMV。我想如果你尝试传递 UITableViewCell 数组或其他一些非常复杂的对象,它会完全崩溃,要么崩溃,要么返回完全垃圾。同样,如果您要执行诸如传递一个 NSDate 数组作为引用数组和一个 NSString 数组作为源数组之类的操作。此外,如果源数组包含引用数组中未涵盖的项目,它们将被丢弃。人们可以通过添加一些额外的代码来解决其中一些问题。话虽如此,如果您尝试做一些简单的事情,它应该会很好地工作。在您的情况下,您可以在循环
setValue:forKey:
时构建引用数组。然后,当您想按照项目出现的顺序循环遍历项目时,您只需
I wrote a quick method to take a source array (of objects that are all out of order) and a reference array (that has objects in a desired (and totally arbitrary) order), and returns an array where the items of the source array have been reorganized to match the reference array.
Note that this is very fragile. It uses
NSArray
'scontainsObject:
method, which ultimately will callNSObject
'sisEqual:
. Basically, it should work great for arrays ofNSString
s,NSNumber
s, and maybeNSDate
s (haven't tried that one yet), but outside of that, YMMV. I imagine if you tried to pass arrays ofUITableViewCell
s or some other really complex object, it would totally sh*t itself, and either crash or return total garbage. Likewise if you were to do something like pass an array ofNSDate
s as the reference array and an array ofNSString
s as the source array. Also, if the source array contains items not covered in the reference array, they'll just get discarded. One could address some of these issues by adding a little extra code.All that said, if you're trying to do something simple, it should work nicely. In your case, you could build up the reference array as you are looping through your
setValue:forKey:
.Then, when you want to loop over your items in the order they came in, you just
实际上你有一个按顺序引用的数组,那么为什么你必须添加一个数组。所以我想这种方法不好。请考虑我的意见。
Actually you have a reference array in order manner then why you have to add to one more array.So i guess this approach is not good.Please consider my opinion.
尽管 @GenralMike 的答案很简单,但可以通过省略不必要的代码来优化它,如下所示:
1)保留一个数组以按添加顺序保存对字典键的引用。
2)现在“referenceArray”按顺序保存所有键,因此您可以按照最初添加到字典中的顺序从字典中检索对象。
Although @GenralMike 's answer works a breeze, it could be optimized by leaving off the unnecessary code as follows:
1) Keep an array to hold reference to the dictionary keys in the order they are added.
2) Now the "referenceArray" holds all of the keys in order, So you can retrieve objects from your dictionary in the same order as they were originally added to the dictionary.