blob: 9fc9261223ca589e54cd42cc4e04db185abfeaca (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<div class="subsection-level-extent" id="Fast-enumeration-details"> <div class="nav-panel"> <p> Next: <a href="fast-enumeration-protocol" accesskey="n" rel="next">Fast Enumeration Protocol</a>, Previous: <a href="c99-like-fast-enumeration-syntax" accesskey="p" rel="prev">C99-Like Fast Enumeration Syntax</a>, Up: <a href="fast-enumeration" accesskey="u" rel="up">Fast Enumeration</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="subsection" id="Fast-Enumeration-Details"><span>8.9.3 Fast Enumeration Details<a class="copiable-link" href="#Fast-Enumeration-Details"> ¶</a></span></h1> <p>Here is a more technical description with the gory details. Consider the code </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">for (<var class="var">object expression</var> in <var class="var">collection expression</var>)
{
<var class="var">statements</var>
}</pre>
</div> <p>here is what happens when you run it: </p> <ul class="itemize mark-bullet"> <li>
<code class="code"><var class="var">collection expression</var></code> 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 <code class="code">for (object in [NSDictionary
keyEnumerator]) …</code>. </li>
<li>the iteration is implemented by the compiler by repeatedly getting batches of objects from the collection object using the fast enumeration protocol (see below), then iterating over all objects in the batch. This is faster than a normal enumeration where objects are retrieved one by one (hence the name “fast enumeration”). </li>
<li>if there are no objects in the collection, then <code class="code"><var class="var">object expression</var></code> is set to <code class="code">nil</code> and the loop immediately terminates. </li>
<li>if there are objects in the collection, then for each object in the collection (in the order they are returned) <code class="code"><var class="var">object expression</var></code> is set to the object, then <code class="code"><var class="var">statements</var></code> are executed. </li>
<li>
<code class="code"><var class="var">statements</var></code> can contain <code class="code">break</code> and <code class="code">continue</code> commands, which will abort the iteration or skip to the next loop iteration as expected. </li>
<li>when the iteration ends because there are no more objects to iterate over, <code class="code"><var class="var">object expression</var></code> is set to <code class="code">nil</code>. This allows you to determine whether the iteration finished because a <code class="code">break</code> command was used (in which case <code class="code"><var class="var">object expression</var></code> will remain set to the last object that was iterated over) or because it iterated over all the objects (in which case <code class="code"><var class="var">object expression</var></code> will be set to <code class="code">nil</code>). </li>
<li>
<code class="code"><var class="var">statements</var></code> must not make any changes to the collection object; if they do, it is a hard error and the fast enumeration terminates by invoking <code class="code">objc_enumerationMutation</code>, a runtime function that normally aborts the program but which can be customized by Foundation libraries via <code class="code">objc_set_mutation_handler</code> to do something different, such as raising an exception. </li>
</ul> </div> <div class="nav-panel"> <p> Next: <a href="fast-enumeration-protocol">Fast Enumeration Protocol</a>, Previous: <a href="c99-like-fast-enumeration-syntax">C99-Like Fast Enumeration Syntax</a>, Up: <a href="fast-enumeration">Fast Enumeration</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Fast-enumeration-details.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Fast-enumeration-details.html</a>
</p>
</div>
|