ios 有没有办法遍历字典?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1284429/
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-30 16:17:58  来源:igfitidea点击:

Is there a way to iterate over a dictionary?

iosobjective-cforeachiterationnsdictionary

提问by Adam Rosenfield

I know NSDictionariesas something where you need a keyin order to get a value. But how can I iterate over all keysand valuesin a NSDictionary, so that I know what keys there are, and what values there are? I know there is something called a for-in-loopin JavaScript. Is there something similar in Objective-C?

我知道NSDictionaries你需要 akey才能获得value. 但是我如何遍历 allkeysvaluesa NSDictionary,以便我知道有哪些键以及有哪些值?我知道有个东西叫一个用于在回路JavaScript。有没有类似的东西Objective-C

回答by Adam Rosenfield

Yes, NSDictionarysupports fast enumeration. With Objective-C 2.0, you can do this:

是的,NSDictionary支持快速枚举。使用 Objective-C 2.0,你可以这样做:

// To print out all key-value pairs in the NSDictionary myDict
for(id key in myDict)
    NSLog(@"key=%@ value=%@", key, [myDict objectForKey:key]);

The alternate method (which you have to use if you're targeting Mac OS X pre-10.5, but you can still use on 10.5 and iPhone) is to use an NSEnumerator:

另一种方法(如果您的目标是 Mac OS X 10.5 之前的版本,则必须使用该方法,但您仍然可以在 10.5 和 iPhone 上使用)是使用NSEnumerator

NSEnumerator *enumerator = [myDict keyEnumerator];
id key;
// extra parens to suppress warning about using = instead of ==
while((key = [enumerator nextObject]))
    NSLog(@"key=%@ value=%@", key, [myDict objectForKey:key]);

回答by Rok Strni?a

The block approachavoids running the lookup algorithm for every key:

块方法避免为每个键运行查找算法:

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
  NSLog(@"%@ => %@", key, value);
}];

Even though NSDictionaryis implemented as a hashtable(which means that the cost of looking up an element is O(1)), lookups still slow down your iteration by a constant factor.

即使NSDictionary是作为一个哈希表(这意味着查找元素的费用O(1)),查找仍然通过你的迭代放慢一个常数因子

My measurements show that for a dictionary dof numbers ...

我的测量表明,对于d数字字典......

NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for (int i = 0; i < 5000000; ++i) {
  NSNumber* value = @(i);
  dict[value.stringValue] = value;
}

... summing up the numbers with the block approach ...

...用块方法总结数字...

__block int sum = 0;
[dict enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSNumber* value, BOOL* stop) {
  sum += value.intValue;
}];

... rather than the loop approach ...

... 而不是循环方法 ...

int sum = 0;
for (NSString* key in dict)
  sum += [dict[key] intValue];

... is about 40% faster.

...快了大约 40%

EDIT: The new SDK (6.1+) appears to optimise loop iteration, so the loop approach is now about 20% faster than the block approach, at least for the simple case above.

编辑:新的 SDK (6.1+) 似乎优化了循环迭代,所以循环方法现在比块方法快 20%,至少对于上面的简单情况。

回答by Javier Calatrava Llavería

This is iteration using block approach:

这是使用块方法的迭代:

    NSDictionary *dict = @{@"key1":@1, @"key2":@2, @"key3":@3};

    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"%@->%@",key,obj);
        // Set stop to YES when you wanted to break the iteration.
    }];

With autocompletion is very fast to set, and you do not have to worry about writing iteration envelope.

自动完成设置非常快,您不必担心编写迭代信封。