What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?
The stop
argument to the Block allows you to stop the enumeration prematurely. It's the equivalent of break
from a normal for
loop. You can ignore it if you want to go through every 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; // Stop enumerating return; } if( ![obj isKindOfClass:[Perefrigia class]] ){ return; // Skip this object } [obj immanentizeTheEschaton];}];
That is an out parameter because it is a reference to a variable from the calling scope. It needs to be set inside your Block, but read inside of enumerateObjectsUsingBlock:
, the same way NSError
s are commonly passed back to your code from framework calls.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block { // N.B: 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; } }}