gcc: Fast enumeration details
1
1 8.9.3 Fast Enumeration Details
1 ------------------------------
1
1 Here is a more technical description with the gory details. Consider
1 the code
1
1 for (OBJECT EXPRESSION in COLLECTION EXPRESSION)
1 {
1 STATEMENTS
1 }
1
1 here is what happens when you run it:
1
1 * 'COLLECTION EXPRESSION' is evaluated exactly once and the result is
1 used as the collection object to iterate over. This means it is
1 safe to write code such as 'for (object in [NSDictionary
1 keyEnumerator]) ...'.
1
1 * the iteration is implemented by the compiler by repeatedly getting
1 batches of objects from the collection object using the fast
1 enumeration protocol (see below), then iterating over all objects
1 in the batch. This is faster than a normal enumeration where
1 objects are retrieved one by one (hence the name "fast
1 enumeration").
1
1 * if there are no objects in the collection, then 'OBJECT EXPRESSION'
1 is set to 'nil' and the loop immediately terminates.
1
1 * if there are objects in the collection, then for each object in the
1 collection (in the order they are returned) 'OBJECT EXPRESSION' is
1 set to the object, then 'STATEMENTS' are executed.
1
1 * 'STATEMENTS' can contain 'break' and 'continue' commands, which
1 will abort the iteration or skip to the next loop iteration as
1 expected.
1
1 * when the iteration ends because there are no more objects to
1 iterate over, 'OBJECT EXPRESSION' is set to 'nil'. This allows you
1 to determine whether the iteration finished because a 'break'
1 command was used (in which case 'OBJECT EXPRESSION' will remain set
1 to the last object that was iterated over) or because it iterated
1 over all the objects (in which case 'OBJECT EXPRESSION' will be set
1 to 'nil').
1
1 * 'STATEMENTS' must not make any changes to the collection object; if
1 they do, it is a hard error and the fast enumeration terminates by
1 invoking 'objc_enumerationMutation', a runtime function that
1 normally aborts the program but which can be customized by
1 Foundation libraries via 'objc_set_mutation_handler' to do
1 something different, such as raising an exception.
1