working with a basic objective c example here, tried to use replaceObjectAtIndex for an array and it doesn’t seem to be working.
my code:
NSMutableArray *myArray=[NSMutableArray array];
[myArray addObject:@"First string"];
[myArray addObject:@"Second string"];
[myArray addObject:@"Third string"];
NSString *newElement=[myArray objectAtIndex:1];
NSLog(@"New object at index 1 BEFORE is %@", newElement);
[myArray replaceObjectAtIndex:1 withObject:@"Hello"];
NSLog(@"New object at index 1 AFTER is %@", newElement);
theoretically the output for newElement should now display “Hello”, but it’s still displaying “Second String”
output:
2012-05-30 11:21:16.638 cocoa lab[753:403] New object at index 1 BEFORE is Second string
2012-05-30 11:21:16.641 cocoa lab[753:403] New object at index 1 AFTER is Second string
please advise
thank you
You need to get the new value from the array after replacing it
At the moment you fetch the original string, replace the array’s object, and print out the original string again.