iOS:替换数组数组中的对象

Cra*_*Dev 3 xcode objective-c nsarray ios

我的代码是:

[[arrayOne objectAtIndex:indexSelected] replaceObjectAtIndex:1 withObject:new];


NSLog(@"indexSelected:%d", indexSelected); // = 0
NSLog(@"new:%@", new); // = 26
Run Code Online (Sandbox Code Playgroud)

indexSelected是一个int并且new是一个字符串

当我尝试这样做时,我有一个例外,说: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x1cee60'

为什么?

Jos*_*ith 6

这是因为NSArray是不可变的.使用NSMutableArray.