|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
-
- Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3 or
- any later version published by the Free Software Foundation; with the
- Invariant Sections being "Funding Free Software", the Front-Cover
- Texts being (a) (see below), and with the Back-Cover Texts being (b)
- (see below). A copy of the license is included in the section entitled
- "GNU Free Documentation License".
-
- (a) The FSF's Front-Cover Text is:
-
- A GNU Manual
-
- (b) The FSF's Back-Cover Text is:
-
- You have freedom to copy and modify this GNU Manual, like GNU
- software. Copies published by the Free Software Foundation raise
- funds for GNU development. -->
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Fast enumeration details (Using the GNU Compiler Collection (GCC))</title>
-
- <meta name="description" content="Fast enumeration details (Using the GNU Compiler Collection (GCC))">
- <meta name="keywords" content="Fast enumeration details (Using the GNU Compiler Collection (GCC))">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
- <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="Fast-enumeration.html#Fast-enumeration" rel="up" title="Fast enumeration">
- <link href="Fast-enumeration-protocol.html#Fast-enumeration-protocol" rel="next" title="Fast enumeration protocol">
- <link href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax" rel="prev" title="c99-like fast enumeration syntax">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="Fast-enumeration-details"></a>
- <div class="header">
- <p>
- Next: <a href="Fast-enumeration-protocol.html#Fast-enumeration-protocol" accesskey="n" rel="next">Fast enumeration protocol</a>, Previous: <a href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax" accesskey="p" rel="prev">c99-like fast enumeration syntax</a>, Up: <a href="Fast-enumeration.html#Fast-enumeration" accesskey="u" rel="up">Fast enumeration</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
- </div>
- <hr>
- <a name="Fast-Enumeration-Details"></a>
- <h4 class="subsection">8.9.3 Fast Enumeration Details</h4>
-
- <p>Here is a more technical description with the gory details. Consider the code
- </p>
- <div class="smallexample">
- <pre class="smallexample"> for (<var>object expression</var> in <var>collection expression</var>)
- {
- <var>statements</var>
- }
- </pre></div>
-
- <p>here is what happens when you run it:
- </p>
- <ul>
- <li> <code><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>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><var>object expression</var></code> is set to <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><var>object expression</var></code>
- is set to the object, then <code><var>statements</var></code> are executed.
-
- </li><li> <code><var>statements</var></code> can contain <code>break</code> and <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><var>object expression</var></code> is set to <code>nil</code>. This allows
- you to determine whether the iteration finished because a <code>break</code>
- command was used (in which case <code><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><var>object expression</var></code> will be
- set to <code>nil</code>).
-
- </li><li> <code><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>objc_enumerationMutation</code>, a runtime
- function that normally aborts the program but which can be customized
- by Foundation libraries via <code>objc_set_mutation_handler</code> to do
- something different, such as raising an exception.
-
- </li></ul>
-
- <hr>
- <div class="header">
- <p>
- Next: <a href="Fast-enumeration-protocol.html#Fast-enumeration-protocol" accesskey="n" rel="next">Fast enumeration protocol</a>, Previous: <a href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax" accesskey="p" rel="prev">c99-like fast enumeration syntax</a>, Up: <a href="Fast-enumeration.html#Fast-enumeration" accesskey="u" rel="up">Fast enumeration</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
- </div>
-
-
-
- </body>
- </html>
|