Know the index with fast enumeration?


#1

When using fast enumeration is it possible to determine the index?


#2

Good question. This looks like a good conversation on the topic: stackoverflow.com/questions/8113 … numeration


#3

Wow, blocks look cool but I’m going to wait until I get to that chapter.

In the meantime, I’m wondering if the efficiency of fast enumeration is lost if you manage an index (increment a counter) in the loop. In other words, if you’re not ready for blocks yet and you need the index, should you just use an ordinary for loop?


#4

Apple suggests just using a counter:

developer.apple.com/library/mac/ … ation.html

“For collections or enumerators that have a well-defined order—such as an NSArray or an NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations gives you the proper index into the collection if you need it.” You still get the same benefits of fast enumeration and immutability of the array.