快速枚举(循环)在Objective-C中如何工作? (即:for(NSString * aString in aDictionary)...) [英] How does Fast Enumeration (looping) work in Objective-C? (ie: for (NSString *aString in aDictionary)...)

查看:108
本文介绍了快速枚举(循环)在Objective-C中如何工作? (即:for(NSString * aString in aDictionary)...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个相当复杂的表实现一个自定义的searchBar,并且已经遇到了这个代码模式。这是从开始的iPhone开发书中的一个示例:

   - (void)handleSearchForTerm:(NSString *)searchTerm 
{
NSMutableArray * sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for(NSString * key in self.keys)
{
NSMutableArray * array = [self.names valueForKey:key];
NSMutableArray * toRemove = [[NSMutableArray alloc] init];
for(NSString * name in array)
{
if([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch] .location == NSNotFound)
[toRemove addObject:名称];
}

if([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}

我很好奇的部分是for(NSString * name in数组)部分。这是做什么呢?似乎为数组中的每个项创建一个字符串。另外,这是如何与字典工作的?



谢谢!

解决方案

这个结构是一种不同类型的,用于循环,它运行在Objective-C集合中的项目上,而不是C数组。第一部分定义一个对象,该对象被设置为每个循环运行的集合中的一个元素,而第二部分是枚举的集合。例如,代码:

  NSArray * array = [NSArray arrayWithObjects:@foo,@bar,nil] ; 
for(NSString * string in array){
NSLog(string);
}

将打印:

 foo 
bar

它定义了一个 NSString * string ,每次运行循环,设置为 NSArray *数组中的下一个对象。



同样,您可以使用枚举与实例的NSSet(其中没有定义对象的顺序)和NSDictionary(它将枚举在字典中存储的密钥) - 您可以通过枚举关键字枚举值,然后调用 valueForKey:

它与C中的构造非常相似:

  int array [2] = {0,1}; (int i = 0; i <2; i ++){
printf(%d\\\
,array [i]);

}

打印:

 0 
1

这只是一种语法方式,使代码更可读,并隐藏了一些花哨的枚举, NSArray,NSSet或NSDictionary。更多细节在The 快速枚举部分中给出Objective-C 2.0编程语言文档。


I'm working on implementing a customized searchBar for a fairly complex table and have come across this code pattern AGAIN. This is a sample from the Beginning iPhone Development book:

- (void)handleSearchForTerm:(NSString *)searchTerm
 {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for (NSString *key in self.keys)
  {
    NSMutableArray *array = [self.names valueForKey:key];
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (NSString *name in array)
    {
        if ([name rangeOfString:searchTerm
                      options:NSCaseInsensitiveSearch].location == NSNotFound)
            [toRemove addObject:name];
    }

    if ([array count] == [toRemove count])
        [sectionsToRemove addObject:key];
    [array removeObjectsInArray:toRemove];
    [toRemove release];
  }
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}

The part I'm curious about is the "for (NSString *name in array)" section. What is this doing exactly? It seems to create a string for every item in the array. Also, how does this work with dictionaries?

Thanks!

解决方案

This construct is a different kind of for loop that runs over items in an Objective-C collection, rather than a C array. The first part defines an object that is being set to one element in the collection each run of the loop, while the second part is the collection to enumerate. For example, the code:

NSArray *array = [NSArray arrayWithObjects:@"foo", @"bar", nil];
for(NSString *string in array) {
    NSLog(string);
}

would print:

foo
bar

It's defining an NSString *string that, each run of the loop, gets set to the next object in the NSArray *array.

Similarly, you can use enumeration with instances of NSSet (where the order of objects aren't defined) and NSDictionary (where it will enumerate over keys stored in the dictionary - you can enumerate over the values by enumerating over keys, then calling valueForKey: on the dictionary using that key).

It's extremely similar to the construct in C:

int array[2] = { 0, 1 };
for(int i = 0; i < 2; i++) {
    printf("%d\n", array[i]);
}

which prints:

0
1

It's just a syntactical way of making the code more readable and hiding some of the fancy enumeration that goes into listing objects in an NSArray, NSSet, or NSDictionary. More detail is given in the Fast Enumeration section of The Objective-C 2.0 Programming Language document.

这篇关于快速枚举(循环)在Objective-C中如何工作? (即:for(NSString * aString in aDictionary)...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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