blob: f212bd86124729b9cf62338914e5b4017d094747 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<div class="subsection-level-extent" id="Using-fast-enumeration"> <div class="nav-panel"> <p> Next: <a href="c99-like-fast-enumeration-syntax" accesskey="n" rel="next">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="Using-Fast-Enumeration"><span>8.9.1 Using Fast Enumeration<a class="copiable-link" href="#Using-Fast-Enumeration"> ¶</a></span></h1> <p>GNU Objective-C provides support for the fast enumeration syntax: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">id array = …;
id object;
for (object in array)
{
/* Do something with 'object' */
}</pre>
</div> <p><code class="code">array</code> needs to be an Objective-C object (usually a collection object, for example an array, a dictionary or a set) which implements the “Fast Enumeration Protocol” (see below). If you are using a Foundation library such as GNUstep Base or Apple Cocoa Foundation, all collection objects in the library implement this protocol and can be used in this way. </p> <p>The code above would iterate over all objects in <code class="code">array</code>. For each of them, it assigns it to <code class="code">object</code>, then executes the <code class="code">Do something with 'object'</code> statements. </p> <p>Here is a fully worked-out example using a Foundation library (which provides the implementation of <code class="code">NSArray</code>, <code class="code">NSString</code> and <code class="code">NSLog</code>): </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">NSArray *array = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];
NSString *object;
for (object in array)
NSLog (@"Iterating over %@", object);</pre>
</div> </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/Using-fast-enumeration.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Using-fast-enumeration.html</a>
</p>
</div>
|