Ios 由于数组结构中的数组,Objective-c replaceObjectAtIndex不工作

Ios 由于数组结构中的数组,Objective-c replaceObjectAtIndex不工作,ios,objective-c,nsarray,Ios,Objective C,Nsarray,我有一个数组,通过以下代码获得值: NSString *status = [[Sweetresponse objectAtIndex:path.row] objectAtIndex:9]; 我想更改此值: [[Sweetresponse objectAtIndex:path.row] replaceObjectAtIndex:9 withObject:@"liked"]; 但它不起作用,因为此结构是数组中的数组。如何解决此问题?这是因为NSArray是不可变的。你必须使它可变 NSMutab

我有一个数组,通过以下代码获得值:

NSString *status = [[Sweetresponse objectAtIndex:path.row] objectAtIndex:9];
我想更改此值:

[[Sweetresponse objectAtIndex:path.row] replaceObjectAtIndex:9 withObject:@"liked"];

但它不起作用,因为此结构是数组中的数组。如何解决此问题?

这是因为
NSArray
是不可变的。你必须使它可变

NSMutableArray *mutableResponse = [Sweetresponse mutableCopy];
NSMutableArray *mutableResponseItems = [[mutableResponse objectAtIndex:path.row] mutableCopy];
// replace at the index
[mutableResponseItems replaceObjectAtIndex:9 withObject:@"liked"];
// create immutables and replace it with our new array
mutableResponse[path.row] = [mutableResponseItems copy];
// set `Sweetresponse` (assuming it is an NSArray)
Sweetresponse = [mutableResponse copy];
编辑:我不知道什么是
Sweetresponse
,这里我假设它是
NSArray


@trojanfoe的一个优点是,将此响应解析为自定义对象将导致代码更清晰,对象修改也更简单。

保留一个自定义对象数组。从长远来看会更好。它不起作用吗?您是否收到任何错误/崩溃/其他信息?***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[\uu nsFarray ReplaceObjectIndex:withObject::::]:将方法发送到不可变对象“仅供参考”-请遵循标准命名约定。只有类名应该以大写字母开头。方法名和变量名应始终以小写字母开头。
mutableCopy
?你确定吗?是的,
mutableCopy
返回一个可变的新实例。创建该实例的非可变副本以恢复不变性很重要。您可以在前面创建可变数组的可变数组,然后再不进行复制。虽然您是对的,但不鼓励这样做,因为您可能会得到奇怪的值,而且争用条件在可变对象中也很常见。真的吗?这对我来说是个新闻。