Name

NSEnumerator — Mac OS X 10.0

Synopsis

This class enumerates the contents of a collection. Instances of this class are created by the collection classes, generally with the method objectEnumerator. NSArray declares the method reverseObjectEnumerator, and NSDictionary declares keyEnumerator as additional methods that create instances of NSEnumerator.

To obtain the next object in an enumerator, we invoke the method nextObject. This will return an object, and advance the enumerator position forward in the collection. When the enumerator has exhausted the collection, nextObject returns nil. Enumerators cannot be reset; once you have enumerated a collection, you must obtain an new enumerator from the collection to start again. The method allObjects is used to return an NSArray of the objects that have not yet been enumerated. See Chapter 2 for more information on collections and enumerators.

image with no caption

@interface NSEnumerator : NSObject
                                  // Instance Methods
   - (NSArray *)allObjects;
   - (id)nextObject;

Subclasses

NSDirectoryEnumerator

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.