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

2.9.3 Replacing an Object

To replace an object, you can use replaceObjectAtIndex:withObject:, as in
NSMutableArray *array;

array = [NSMutableArray new];
[array addObject: @"Alessia"];
[array addObject: @"Michele"];

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

[array replaceObjectAtIndex: 1  withObject: @"Nicola"];

/* Now the array contains Alessia, Nicola. */
The object which is removed from the array (because it is being replaced) receives a release message; the object which is added to the array (because it replaces the other object) receives a retain message.