The stop argument to a block allows you to prematurely stop the enumeration. This is equivalent to break from a normal for loop. You can ignore it if you want to go through each object in the array.
for( id obj in arr ){ if( [obj isContagious] ){ break; // Stop enumerating } if( ![obj isKindOfClass:[Perefrigia class]] ){ continue; // Skip this object } [obj immanetizeTheEschaton]; }
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if( [obj isContagious] ){ *stop = YES;
This is an out parameter because it is a reference to a variable from the scope of the call. It should be installed inside your block, but read inside enumerateObjectsUsingBlock: like NSError , it is usually passed back to your code from infrastructure calls.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block { // NB: This is probably not how this method is actually implemented! // It is just to demonstrate how the out parameter operates! NSUInteger idx = 0; for( id obj in self ){ BOOL stop = NO; block(obj, idx++, &stop); if( stop ){ break; } } }
Josh Caswell Sep 10 '12 at 19:09 2012-09-10 19:09
source share