Next: Removing an Object Up: NSMutableArray Previous: NSMutableArray

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. */



Nicola 2002-01-13