NSEnumerator

From GNUstepWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

NSEnumerator in gnustep-base allow for iteration over collection.

Using NSEnumerator

The following example demonstrates the use of NSEnumerator with an NSArray object:

 NSArray *aArray = [NSArray arrayWithObjects: @"John", @"Bob", @"Jane", nil];
 NSEnumerator *enumerator = [aArray objectEnumerator];
 id obj;
 
 while(obj = [enumerator nextObject])
   doSomethingWithObject( obj );

Getting an NSEnumerator from a Collection

The NSArray, NSSet, and NSDictionary collection objects all include the -objectEnumerator method. NSDictionary also includes -keyEnumerator, which enumerates over the dictionary keys instead of the objects.