/GCC 4

8.9.1 Using fast enumeration

GNU Objective-C provides support for the fast enumeration syntax:

id array = ...;
id object;
for (object in array)
  /* Do something with 'object' */

array 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.

The code above would iterate over all objects in array. For each of them, it assigns it to object, then executes the Do something with 'object' statements.

Here is a fully worked-out example using a Foundation library (which provides the implementation of NSArray, NSString and NSLog):

NSArray *array = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];
NSString *object;
for (object in array)
  NSLog (@"Iterating over %@", object);

© Free Software Foundation
Licensed under the GNU Free Documentation License, Version 1.3.