Enumerating using blocks
suggest changeNSArray *myColors = @[@"Red", @"Green", @"Blue", @"Yellow"];
[myColors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"enumerating object %@ at index %lu", obj, (unsigned long)idx);
}];
Set *stop = YES to end enumeration early. stop is a pointer; assigning YES to the pointer itself is not valid.
NSEnumerationOptions
You can enumerate in reverse, or concurrently. Do not combine NSEnumerationReverse and NSEnumerationConcurrent.
[myColors enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"enumerating object %@ at index %lu", obj, (unsigned long)idx);
}];
Enumerating subset of array
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 1)];
[myColors enumerateObjectsAtIndexes:indexSet
options:kNilOptions
usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"enumerating object %@ at index %lu", obj, idx);
}];
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents