Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSMutableArray

Inherits From: NSArray : NSObject

Conforms To: NSCoding, NSCopying, NSMutableCopying (NSArray) NSObject (NSObject)

Declared In: Foundation/NSArray.h

Class Description

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior it inherits from NSArray.

The array operations that NSMutableArray declares are conceptually based on these three methods:

addObject:

replaceObjectAtIndex:withObject:

removeLastObject

The other methods in its interface provide convenient ways of inserting an object into a specific slot in the array and of removing an object based on its identity or position in the array.

When an object is removed from a mutable array it receives a release message, which can cause it to be deallocated. Note that if your program keeps a reference to such an object, the reference may become invalid unless you remember to send the object a retain message before it's removed from the array. For example, the third statement below could result in a run-time error, except for the retain message in the first statement:

id anObject = [[anArray objectAtIndex:0] retain];

[anArray removeObjectAtIndex:0];

[anObject someMessage];

Implementing Subclasses of NSMutableArray

Although conceptually the interface to the NSMutableArray class is based on the three methods listed above, for performance reasons two othersinsertObject:atIndex: and removeObjectAtIndex:also directly access the object's data. These two methods could be implemented using the methods listed above but in doing so would incur unnecessary overhead from the retain and release messages that objects would receive as they are shifted to accommodate the insertion or deletion of an element. Thus, if you create a subclass of NSMutableArray, you should override all five primitive methods so that the other methods in NSMutableArray's interface work properly.

Creating and Initializing an NSMutableArray

Adding Objects

Removing Objects

Replacing Objects