ios 使用快速枚举和 NSDictionary,不能保证按键的顺序迭代——我怎样才能让它按顺序进行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17960068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:33:42  来源:igfitidea点击:

With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

iosobjective-cnsdictionaryfor-in-loopfast-enumeration

提问by Doug Smith

I'm communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the front of the NSDictionary.

我正在与一个 API 通信,该 API 将 NSDictionary 作为响应发送回我的应用程序需要的数据(数据基本上是一个提要)。这些数据按从新到旧排序,最新的项目在 NSDictionary 的前面。

When I fast enumerate through them with for (NSString *key in articles) { ... }the order is seemingly random, and thus the order I operate on them isn't in order from newest to oldest, like I want it to be, but completely random instead.

当我快速枚举它们时for (NSString *key in articles) { ... },顺序似乎是随机的,因此我对它们进行操作的顺序不是按照我希望的从最新到最旧的顺序,而是完全随机的。

I've read up, and when using fast enumeration with NSDictionary it is not guaranteed to iterate in order through the array.

我已经阅读了,当使用 NSDictionary 的快速枚举时,不能保证按顺序遍历数组。

However, I needit to. How do I make it iterate through the NSDictionary in the order that NSDictionary is in?

但是,我需要它。如何让它按照 NSDictionary 所在的顺序遍历 NSDictionary?

回答by Mario

One way could be to get all keys in a mutable array:

一种方法是获取可变数组中的所有键:

NSMutableArray *allKeys = [[dictionary allKeys] mutableCopy];

And then sort the array to your needs:

然后根据您的需要对数组进行排序:

[allKeys sortUsingComparator: ....,]; //or another sorting method

You can then iterate over the array (using fast enumeration here keeps the order, I think), and get the dictionary values for the current key:

然后您可以遍历数组(我认为这里使用快速枚举保持顺序),并获取当前键的字典值:

for (NSString *key in allKeys) {
   id object = [dictionary objectForKey: key];
   //do your thing with the object 
 }

回答by rmaddy

Dictionaries are, by definition, unordered. If you want to apply an order to the keys, you need to sort the keys.

根据定义,字典是无序的。如果要对键应用顺序,则需要对键进行排序。

NSArray *keys = [articles allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
for (NSString *key in sortedKeys) {
    // process key
}

Update the way the keys are sorted to suit your needs.

更新键的排序方式以满足您的需要。

回答by Lucas Eduardo

As other people said, you cannot garantee order in NSDictionary. And sometimes ordering the allKeys property it's not what you really want. If what you really want is enumerate your dict by the order your keys were inserted in your dict, you can create a new NSMutableArray property/variable to store your keys, so they will preserve its order.

正如其他人所说,您不能保证 NSDictionary 中的订单。有时订购 allKeys 属性并不是您真正想要的。如果您真正想要的是按照您的键插入到您的 dict 中的顺序枚举您的 dict,您可以创建一个新的 NSMutableArray 属性/变量来存储您的键,因此它们将保留其顺序。

Everytime you will insert a new key in the dict, insert it to in your array:

每次您将在字典中插入一个新键时,将其插入到您的数组中:

[articles addObject:someArticle forKey:@"article1"];
[self.keys addObject:@"article1"];

To enumerate them in order, just do:

要按顺序枚举它们,只需执行以下操作:

for (NSString *key in self.keys) {
   id object = articles[key];
}

回答by Hashim Akhtar

You can also directly enumerate like this:

你也可以像这样直接枚举:

NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];

for (NSString* key in xyz) {
    id value = [xyz objectForKey:key];
    // do stuff
} 



This is taken from the answer provided by zneak in the question: for each loop in objective c for accessing NSMutable dictionary



这取自 zneak 在问题中提供的答案:for each loop in Objective c for accessing NSMutable dictionary