NSMutableArray replaceObjectAtIndex withObject
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
Parameters of [NSMutableArray replaceObjectAtIndex withObject]
index
The index of the object to be replaced. This value must not exceed the bounds of the array.
Important: Raises an NSRangeException if index is beyond the end of the array.
anObject
The object with which to replace the object at index index in the array. This value must not be nil.
Important: Raises an NSInvalidArgumentException if anObject is nil.
NSMutableArray replaceObjectAtIndex withObject example.
- (void) setList:(NSMutableArray*) newList
{
if ( list != newList )
{
[list release];
list = [newList mutableCopy];
}
}
- (void) updateTitle:(NSString*)newTitle forIndex:(NSString*)theIndex
{
int i = [theIndex intValue] - 1;
[self.list replaceObjectAtIndex:i withObject:newTitle];
}
{
if ( list != newList )
{
[list release];
list = [newList mutableCopy];
}
}
- (void) updateTitle:(NSString*)newTitle forIndex:(NSString*)theIndex
{
int i = [theIndex intValue] - 1;
[self.list replaceObjectAtIndex:i withObject:newTitle];
}
Example of [NSMutableArray replaceObjectAtIndex withObject].
// Browse all messages (you can use "for (NSDictionary *message in allMessageArray)" enumerate loop but because we need the index to replace object, it's the best way to do that)
for (int index = 0; index < allMessageArray.count; ++index) {
// Get current message dictionary
NSDictionary *message = [allMessageArray objectAtIndex:index];
// If message came from good sender (you can use isEqualToString: if both objects are NSString instance)
if ([[message objectForKey:@"expediteur"] isEqual:[dicoChat2 objectForKey:@"expediteur"]]) {
// Create an autoreleased mutable copy of message array to modify some data
NSMutableDictionary *messageModified = [NSMutableDictionary dictionaryWithDictionary:message];
// *** Modify what you want in messageModified dictionary ***
// Replace original message with modified message in array (assume that allMessageArray is a mutable array) (It's very bad to modify an array in its enumerate loop but because we don't remove/add an object to the array, it's fine to do like that)
[allMessageArray replaceObjectAtIndex:index withObject:messageModified];
// *** If you know that you will have always only one message in array with good sender, you can break the loop here ***
}
}
// Write array to file
[allMessageArray writeToFile:datAllString atomically:YES];
for (int index = 0; index < allMessageArray.count; ++index) {
// Get current message dictionary
NSDictionary *message = [allMessageArray objectAtIndex:index];
// If message came from good sender (you can use isEqualToString: if both objects are NSString instance)
if ([[message objectForKey:@"expediteur"] isEqual:[dicoChat2 objectForKey:@"expediteur"]]) {
// Create an autoreleased mutable copy of message array to modify some data
NSMutableDictionary *messageModified = [NSMutableDictionary dictionaryWithDictionary:message];
// *** Modify what you want in messageModified dictionary ***
// Replace original message with modified message in array (assume that allMessageArray is a mutable array) (It's very bad to modify an array in its enumerate loop but because we don't remove/add an object to the array, it's fine to do like that)
[allMessageArray replaceObjectAtIndex:index withObject:messageModified];
// *** If you know that you will have always only one message in array with good sender, you can break the loop here ***
}
}
// Write array to file
[allMessageArray writeToFile:datAllString atomically:YES];
NSMutableArray replaceObjectAtIndex withObject example.
if (newObject)
[array replaceObjectAtIndex:0 withObject:newObject];
else{
// handle nil case
[array replaceObjectAtIndex:0 withObject:[NSNull null]];
}
[array replaceObjectAtIndex:0 withObject:newObject];
else{
// handle nil case
[array replaceObjectAtIndex:0 withObject:[NSNull null]];
}