1<html lang="en"> 2<head> 3<title>Using fast enumeration - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Fast-enumeration.html#Fast-enumeration" title="Fast enumeration"> 9<link rel="next" href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax" title="c99-like fast enumeration syntax"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1988-2013 Free Software Foundation, Inc. 13 14Permission is granted to copy, distribute and/or modify this document 15under the terms of the GNU Free Documentation License, Version 1.3 or 16any later version published by the Free Software Foundation; with the 17Invariant Sections being ``Funding Free Software'', the Front-Cover 18Texts being (a) (see below), and with the Back-Cover Texts being (b) 19(see below). A copy of the license is included in the section entitled 20``GNU Free Documentation License''. 21 22(a) The FSF's Front-Cover Text is: 23 24 A GNU Manual 25 26(b) The FSF's Back-Cover Text is: 27 28 You have freedom to copy and modify this GNU Manual, like GNU 29 software. Copies published by the Free Software Foundation raise 30 funds for GNU development.--> 31<meta http-equiv="Content-Style-Type" content="text/css"> 32<style type="text/css"><!-- 33 pre.display { font-family:inherit } 34 pre.format { font-family:inherit } 35 pre.smalldisplay { font-family:inherit; font-size:smaller } 36 pre.smallformat { font-family:inherit; font-size:smaller } 37 pre.smallexample { font-size:smaller } 38 pre.smalllisp { font-size:smaller } 39 span.sc { font-variant:small-caps } 40 span.roman { font-family:serif; font-weight:normal; } 41 span.sansserif { font-family:sans-serif; font-weight:normal; } 42--></style> 43<link rel="stylesheet" type="text/css" href="../cs.css"> 44</head> 45<body> 46<div class="node"> 47<a name="Using-fast-enumeration"></a> 48<p> 49Next: <a rel="next" accesskey="n" href="c99_002dlike-fast-enumeration-syntax.html#c99_002dlike-fast-enumeration-syntax">c99-like fast enumeration syntax</a>, 50Up: <a rel="up" accesskey="u" href="Fast-enumeration.html#Fast-enumeration">Fast enumeration</a> 51<hr> 52</div> 53 54<h4 class="subsection">8.9.1 Using fast enumeration</h4> 55 56<p>GNU Objective-C provides support for the fast enumeration syntax: 57 58<pre class="smallexample"> id array = ...; 59 id object; 60 61 for (object in array) 62 { 63 /* Do something with 'object' */ 64 } 65</pre> 66 <p><code>array</code> needs to be an Objective-C object (usually a collection 67object, for example an array, a dictionary or a set) which implements 68the “Fast Enumeration Protocol” (see below). If you are using a 69Foundation library such as GNUstep Base or Apple Cocoa Foundation, all 70collection objects in the library implement this protocol and can be 71used in this way. 72 73 <p>The code above would iterate over all objects in <code>array</code>. For 74each of them, it assigns it to <code>object</code>, then executes the 75<code>Do something with 'object'</code> statements. 76 77 <p>Here is a fully worked-out example using a Foundation library (which 78provides the implementation of <code>NSArray</code>, <code>NSString</code> and 79<code>NSLog</code>): 80 81<pre class="smallexample"> NSArray *array = [NSArray arrayWithObjects: @"1", @"2", @"3", nil]; 82 NSString *object; 83 84 for (object in array) 85 NSLog (@"Iterating over %@", object); 86</pre> 87 <!-- ================================ --> 88 </body></html> 89 90