I’ve been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I’m having a hard time understanding the usage of BOOL *stop in the enumeration block.
The NSArray class reference states
stop: A reference to a Boolean value. The block can set the value toYESto
stop further processing of the array. Thestopargument is an out-only
argument. You should only ever set this Boolean toYESwithin the
Block.
So then of course I can add the following in my block to stop the enumeration:
if (idx == [myArray indexOfObject:[myArray lastObject]]) {
*stop = YES;
}
From what I’ve been able to tell, not explicitly setting *stop to YES doesn’t have any negative side effects. The enumeration seems to automatically stop itself at the end of the array. So is using *stop really necessary in a block?
The
stopargument to the Block allows you to stop the enumeration prematurely. It’s the equivalent ofbreakfrom a normalforloop. You can ignore it if you want to go through every object in the array.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 wayNSErrors are commonly passed back to your code from framework calls.