Next: 2.9.2 Removing an Object Up: 2.9 NSMutableArray Previous: 2.9 NSMutableArray

2.9.1 Adding an Object

To add an element at the end of the array, you can use addObject, as in:
NSMutableArray *array;

array = [NSMutableArray new];
[array addObject: anObject];

Assuming anObject is an NSObject (but not nil, remember, you can't put a nil object into an NSArray). As usual, anObject is RETAINed when it is added to the array.

If you want to insert an object into an array at a certain position, you can use insertObject:atIndex::

NSMutableArray *array;

array = [NSMutableArray new];
[array addObject: @"Michele"];
[array addObject: @"Nicola"];
[array insertObject: @"Alessia" atIndex: 1];

/* Now the array contains Michele, Alessia, Nicola. */



2008-01-16