NSArray *anArray = /* ... */;
[anArray enumerateObjectsUsingBlock:
^(id object, NSUInteger idx, BOOL *stop){
// Do something with 'object'
if (shouldStop)
{
*stop = YES;
}
}];
// Dictionary
NSDictionary *aDictionary = /* ... */;
[aDictionary enumerateKeysAndObjectsUsingBlock:
^(id key, id object, BOOL *stop){
// Do something with 'key' and 'object'
if (shouldStop)
{
*stop = YES;
}
}];
// Set
NSSet *aSet = /* ... */;
[aSet enumerateObjectsUsingBlock:
^(id object, BOOL *stop){
// Do something with 'object'
if (shouldStop)
{
*stop = YES;
}
}];
This syntax is slightly more verbose than fast enumeration but is clean, and you get both the index of iteration and the object. This method also provides a clean way to stop the enumeration, if you wish, through the stop variable, although a break can achieve the same thing in the other methods of enumeration and is just as clean.