对于 Objective-C 中用于访问 NSMutable 字典的每个循环

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

for each loop in Objective-C for accessing NSMutable dictionary

objective-cnsdictionaryenumerationnsmutabledictionarykey-value

提问by Sagar R. Kothari

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C.

我发现在 Objective-C 中访问可变字典键和值有些困难。

Suppose I have this:

假设我有这个:

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

I can set keys and values. Now, I just want to access each key and value, but I don't know the number of keys set.

我可以设置键和值。现在,我只想访问每个键和值,但我不知道设置的键数。

In PHP it is very easy, something as follows:

在 PHP 中它很容易,如下所示:

foreach ($xyz as $key => $value)

How is it possible in Objective-C?

在Objective-C中怎么可能?

回答by zneak

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

This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionaryis one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumerationin the Collections Programming Topic.

这适用于符合 NSFastEnumeration 协议的每个类(在 10.5+ 和 iOS 上可用),但它NSDictionary是少数允许您枚举键而不是值的集合之一。我建议您阅读集合编程主题中的快速枚举

Oh, I should add however that you should NEVERmodify a collection while enumerating through it.

哦,我要补充然而,你应该永远不要同时通过它枚举修改的集合。

回答by Quinn Taylor

Just to not leave out the 10.6+ option for enumerating keys and values using blocks...

只是为了不遗漏使用块枚举键和值的 10.6+ 选项......

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

If you want the actions to happen concurrently:

如果您希望操作同时发生:

[dict enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent
                              usingBlock:^(id key, id object, BOOL *stop) {
    NSLog(@"%@ = %@", key, object);
}];

回答by Brody Robertson

If you need to mutatethe dictionary while enumerating:

如果您需要在枚举时改变字典:

for (NSString* key in xyz.allKeys) {
    [xyz setValue:[NSNumber numberWithBool:YES] forKey:key];
}

回答by Avinash

The easiest way to enumerate a dictionary is

枚举字典的最简单方法是

for (NSString *key in tDictionary.keyEnumerator) 
{
    //do something here;
}

where tDictionaryis the NSDictionaryor NSMutableDictionaryyou want to iterate.

你想要迭代tDictionaryNSDictionaryor在哪里NSMutableDictionary

回答by Laurent Etiemble

I suggest you to read the Enumeration: Traversing a Collection's Elementspart of the Collections Programming Guide for Cocoa. There is a sample code for your need.

我建议您阅读Cocoa 集合编程指南枚举:遍历集合的元素部分。有一个示例代码可以满足您的需求。

回答by dreamlax

Fast enumeration was added in 10.5 and in the iPhone OS, and it's significantly faster, not just syntactic sugar. If you have to target the older runtime (i.e. 10.4 and backwards), you'll have to use the old method of enumerating:

在 10.5 和 iPhone OS 中添加了快速枚举,而且速度明显更快,而不仅仅是语法糖。如果您必须针对较旧的运行时(即 10.4 和向后),则必须使用旧的枚举方法:

NSDictionary *myDict = ... some keys and values ...
NSEnumerator *keyEnum = [myDict keyEnumerator];
id key;

while ((key = [keyEnum nextObject]))
{
    id value = [myDict objectForKey:key];
    ... do work with "value" ...
}

You don't release the enumerator object, and you can't reset it. If you want to start over, you have to ask for a new enumerator object from the dictionary.

您不会释放枚举器对象,也无法重置它。如果你想重新开始,你必须从字典中请求一个新的枚举器对象。

回答by gcamp

You can use -[NSDictionary allKeys]to access all the keys and loop through it.

您可以使用-[NSDictionary allKeys]访问所有键并循环遍历它。