When to use enumerateObjectsUsingBlock vs. for
Solution 1:
Ultimately, use whichever pattern you want to use and comes more naturally in the context.
While for(... in ...)
is quite convenient and syntactically brief, enumerateObjectsUsingBlock:
has a number of features that may or may not prove interesting:
enumerateObjectsUsingBlock:
will be as fast or faster than fast enumeration (for(... in ...)
uses theNSFastEnumeration
support to implement enumeration). Fast enumeration requires translation from an internal representation to the representation for fast enumeration. There is overhead therein. Block-based enumeration allows the collection class to enumerate contents as quickly as the fastest traversal of the native storage format. Likely irrelevant for arrays, but it can be a huge difference for dictionaries."Don't use enumerateObjectsUsingBlock when you need to modify local variables" - not true; you can declare your locals as
__block
and they'll be writable in the block.enumerateObjectsWithOptions:usingBlock:
supports either concurrent or reverse enumeration.with dictionaries, block based enumeration is the only way to retrieve the key and value simultaneously.
Personally, I use enumerateObjectsUsingBlock:
more often than for (... in ...)
, but - again - personal choice.
Solution 2:
For simple enumeration, simply using fast enumeration (i.e. a for…in…
loop) is the more idiomatic option. The block method might be marginally faster, but that doesn't matter much in most cases — few programs are CPU-bound, and even then it's rare that the loop itself rather than the computation inside will be a bottleneck.
A simple loop also reads more clearly. Here's the boilerplate of the two versions:
for (id x in y){
}
[y enumerateObjectsUsingBlock:^(id x, NSUInteger index, BOOL *stop){
}];
Even if you add a variable to track the index, the simple loop is easier to read.
So when you should use enumerateObjectsUsingBlock:
? When you're storing a block to execute later or in multiple places. It's good for when you're actually using a block as a first-class function rather than an overwrought replacement for a loop body.