Objective-C Language Enumerating using blocks


Example

NSArray *myColors = @[@"Red", @"Green", @"Blue", @"Yellow"];
[myColors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"enumerating object %@ at index %lu", obj, idx);
}];

By setting the stop parameter to YES you can indicate that further enumeration is not needed. to do this simply set &stop = YES.

NSEnumerationOptions
You can enumerate the array in reverse and / or concurrently :

[myColors enumerateObjectsWithOptions:NSEnumerationConcurrent | NSEnumerationReverse
                               usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                                   NSLog(@"enumerating object %@ at index %lu", obj, 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);
                            }];