You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.9 KiB
HTML
110 lines
4.9 KiB
HTML
<html lang="en">
|
|
<head>
|
|
<title>Fast enumeration details - Using the GNU Compiler Collection (GCC)</title>
|
|
<meta http-equiv="Content-Type" content="text/html">
|
|
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
|
|
<meta name="generator" content="makeinfo 4.13">
|
|
<link title="Top" rel="start" href="index.html#Top">
|
|
<link rel="up" href="Fast-enumeration.html#Fast-enumeration" title="Fast enumeration">
|
|
<link rel="prev" href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax" title="c99-like fast enumeration syntax">
|
|
<link rel="next" href="Fast-enumeration-protocol.html#Fast-enumeration-protocol" title="Fast enumeration protocol">
|
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
|
<!--
|
|
Copyright (C) 1988-2015 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.-->
|
|
<meta http-equiv="Content-Style-Type" content="text/css">
|
|
<style type="text/css"><!--
|
|
pre.display { font-family:inherit }
|
|
pre.format { font-family:inherit }
|
|
pre.smalldisplay { font-family:inherit; font-size:smaller }
|
|
pre.smallformat { font-family:inherit; font-size:smaller }
|
|
pre.smallexample { font-size:smaller }
|
|
pre.smalllisp { font-size:smaller }
|
|
span.sc { font-variant:small-caps }
|
|
span.roman { font-family:serif; font-weight:normal; }
|
|
span.sansserif { font-family:sans-serif; font-weight:normal; }
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
<div class="node">
|
|
<a name="Fast-enumeration-details"></a>
|
|
<p>
|
|
Next: <a rel="next" accesskey="n" href="Fast-enumeration-protocol.html#Fast-enumeration-protocol">Fast enumeration protocol</a>,
|
|
Previous: <a rel="previous" accesskey="p" href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax">c99-like fast enumeration syntax</a>,
|
|
Up: <a rel="up" accesskey="u" href="Fast-enumeration.html#Fast-enumeration">Fast enumeration</a>
|
|
<hr>
|
|
</div>
|
|
|
|
<h4 class="subsection">8.9.3 Fast Enumeration Details</h4>
|
|
|
|
<p>Here is a more technical description with the gory details. Consider the code
|
|
|
|
<pre class="smallexample"> for (<var>object expression</var> in <var>collection expression</var>)
|
|
{
|
|
<var>statements</var>
|
|
}
|
|
</pre>
|
|
<p>here is what happens when you run it:
|
|
|
|
<ul>
|
|
<li><var>collection expression</var> 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>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>if there are no objects in the collection, then
|
|
<var>object expression</var> is set to <code>nil</code> and the loop
|
|
immediately terminates.
|
|
|
|
<li>if there are objects in the collection, then for each object in the
|
|
collection (in the order they are returned) <var>object expression</var>
|
|
is set to the object, then <var>statements</var> are executed.
|
|
|
|
<li><var>statements</var> 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>when the iteration ends because there are no more objects to iterate
|
|
over, <var>object expression</var> 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 <var>object expression</var> will remain
|
|
set to the last object that was iterated over) or because it iterated
|
|
over all the objects (in which case <var>object expression</var> will be
|
|
set to <code>nil</code>).
|
|
|
|
<li><var>statements</var> 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.
|
|
|
|
</ul>
|
|
|
|
<!-- ================================ -->
|
|
</body></html>
|
|
|