NSArray — Mac OS X 10.0


This class manages an immutable ordered collection of objects. Objects are stored in an array by reference. That is, the pointer to the object is stored rather than the object itself. When the object is added to an array, the array retains it by sending a retain message to the object. When the array is released it sends a release message to each of its members.

There are many methods for querying the contents of the array. The method objectAtIndex: is commonly used to access an object at some position in the array. Conversely, we can determine the index of some object using the method indexOfObject:, which returns the lowest index of the member that is equivalent the specified object. To determine the number of objects contained within the array the count method is invoked.

To enumerate the contents of an array, create an NSEnumerator object for the array using one of two methods: objectEnumerator or reverseObjectEnumerator. A standard object enumerator will return the objects in the order that they exist within the array, while the reverse enumerator will return members starting from the last object and working its way forward. See the NSEnumerator class description for more information on enumerating collections.

Often we want to invoke some method in each member of a collection. NSArray provides a method that saves us from the burden of having to enumerate the contents of the array and send the message manually. This method is makeObjectsPerformSelector ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.