이것을 보고 이름하여 Fast Enumeration 이라고 부르는군요. PHP나 Ruby등에서 볼수 있는 foreach와 같은 기능입니다.
일반적인 C에서의 구현에 비해서 매우 편리하게 반복문을 작성할 수 있습니다. 문법은 다음과 같습니다.
// one
for ( Type newVariable in expression ) { statements }
// two
Type existingItem;
for ( existingItem in expression ) { statements }사용 예제를 보시면 바로 이해가 되실 것입니다. 예제를 보실까요.
NSArray
NSArray *array = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];
for (NSString *element in array) {
NSLog(@"element: %@", element);
}
// element: One
// element: Two
// element: Three
// element: FourNSDictionary
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
NSString *key;
for (key in dictionary) {
NSLog(@"English: %@, Latin: %@", key, [dictionary valueForKey:key]);
}
// English: four, Latin: quattuor
// English: five, Latin: quinque
// English: six, Latin: sexNSArray - nextObject
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"안타깝게도 인덱스가 필요하다면 따로 제공하는것이 없어 다음과 같이 사용하셔야 합니다.
NSArray *array = /* assume this exists */;
NSUInteger index = 0;
for (id element in array) {
NSLog(@"Element at index %u is: %@", element);
index++;
}예제만으로도 충분히 만족감을 주는 간단한 내용이었습니다.
이글은 http://theeye.pe.kr/entry/Object-C-Fast-Enumeration 에서도 볼 수 있습니다.


Leave your greetings.