Next: Fast enumeration protocol, Previous: c99-like fast enumeration syntax, Up: Fast enumeration [Contents][Index]
Here is a more technical description with the gory details. Consider the code
for (object expression in collection expression)
{
statements
}
here is what happens when you run it:
collection expression is evaluated exactly once and the
result is used as the collection object to iterate over. This means
it is safe to write code such as for (object in [NSDictionary
keyEnumerator]) ….
object expression is set to nil and the loop
immediately terminates.
object expression
is set to the object, then statements are executed.
statements can contain break and continue
commands, which will abort the iteration or skip to the next loop
iteration as expected.
object expression is set to nil. This allows
you to determine whether the iteration finished because a break
command was used (in which case object expression will remain
set to the last object that was iterated over) or because it iterated
over all the objects (in which case object expression will be
set to nil).
statements must not make any changes to the collection
object; if they do, it is a hard error and the fast enumeration
terminates by invoking objc_enumerationMutation, a runtime
function that normally aborts the program but which can be customized
by Foundation libraries via objc_set_mutation_handler to do
something different, such as raising an exception.
Next: Fast enumeration protocol, Previous: c99-like fast enumeration syntax, Up: Fast enumeration [Contents][Index]