Some arguments against block-based array enumeration

It seems to me that since the introduction of block-based enumeration in Foundation, a fair number of developers have been recommending it as the best technique to use by default. Personally, I disagree (surprise, surprise). I favour fast enumeration by default, using block-based instead for:

  • Classes like NSIndexSet which don't implement NSFastEnumeration
  • Conveniently tracking the index of the current iteration
  • Retrieving both key and value from a dictionary at once (although beware, this can be quite slow)
  • The extra features available, such as concurrency, or enumerating specific indexes


Surprisingly to me, when I went to look, Apple's documentation actually agrees with me, preferring fast enumeration too. (Albeit with reasons that seem slightly out of date since they don't mention block-based enumeration)

My main argument is simply that fast enumeration is easier to debug. Block-based enumeration puts an extra two frames on the stack, making it harder to read.

More importantly, to step through the iterations of a loop, you have to place a breakpoint specifically inside the loop body; with fast enumeration you can have your breakpoint elsewhere, and then conveniently step through iterations. Maybe this is more a question of improving Xcode/LLDB though.

Secondly, as it happens, Fast Enumeration is actually faster for the basic case. I don't know about you, but the simple case of just working my way through all the objects in an array is what I need 90% of the time.

Now don't get me wrong; I'm not arguing against ever using block-based enumeration. I just think we shouldn't try to use it for everything.

© Mike Abdullah 2007-2015