Enumerating using blocks

suggest change
NSArray *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);
                            }];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents