Objective-C 学习笔记 - part 8 - 快速枚举

快速枚举使用的语法:
for ( Type newVariable in expression ) { statements }
or
Type existingItem;
for ( existingItem in expression ) { statements }

枚举期间对象不能被改变。

使用快速枚举的三个类:
NSArray, NSDictionary, NSSet

如何使用:

NSArray *array = [NSArray arrayWithObjects:
        @"One", @"Two", @"Three", @"Four", nil];
 
for (NSString *element in array) {
    NSLog(@"element: %@", element);
}
 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    @"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
 
NSString *key;
for (key in dictionary) {
    NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}

也可以使用 enumeration :
NSArray *array = [NSArray arrayWithObjects:
        @"One", @"Two", @"Three", @"Four", nil];
 
NSEnumerator *enumerator = [array reverseObjectEnumerator];
for (NSString *element in enumerator) {
    if ([element isEqualToString:@"Three"]) {
        break;
    }
}
 
NSString *next = [enumerator nextObject];
// next = "Two"


posted on 2011-08-17 15:27 沙加 阅读(159) 评论(0) 编辑 收藏

导航

公告

昵称:沙加
园龄:5年11个月
粉丝:36
关注:13
<2011年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

搜索

 

我的标签

随笔分类(89)

随笔档案(98)

Atlas相关

积分与排名

最新评论

阅读排行榜

推荐排行榜