在这里使用一个基本的目标 c 示例,尝试将 replaceObjectAtIndex 用于数组,但它似乎不起作用。
我的代码:
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);
理论上,newElement 的输出现在应该显示“Hello”,但它仍然显示“Second String”
输出:
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
请指教
谢谢你