快速枚举是Objective-C 2.0 中提供的一种简洁的特色语法,可以安全又快速地对集合中的元素进行枚举。
标准形式如下:
for (Type newVariable in expression) {
statements;
}
//或者
Type variable;
for (variable in expression) {
statements;
}
举个�
// 使用NSArray的快速枚举
NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
for (NSArray *element in array) {
NSLog(@"element : %@", element);
}
// 使用NSDictionary的快速枚举
NSDictionary *dic = [[NSDictionary alloc] dictionaryWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3", nil];
NSString *key;
for (key in dic) {
NSLog(@"english : %@, number : %@", [dic valueForKey: key], key);
}
本文介绍了Objective-C2.0中的快速枚举语法,这是一种简洁高效地遍历集合的方法。通过两个示例展示了如何使用快速枚举来遍历NSArray和NSDictionary。
2713

被折叠的 条评论
为什么被折叠?



