您好我正在使用下面的代码行,其中我的 responseJson 和 indexpathArray 都是 NSmutableArrays
[responseJson addObject:indexpathArray];
但我正在接受
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
我已引用相关主题Getting "mutating method sent to immutable object" error但还是一样
实际上我在这里尝试将记录从新数组追加到旧数组,每次从服务器获取 10 条记录。
感谢任何帮助!
更新
NSMutableArray *arrayResult = [NSMutableArray arrayWithArray:responseJson];
// Now add objects top arrayResult
[arrayResult addObject:finalarray];
NSLog(@" arrayResult before ::: %@",arrayResult);
responseJson = [[NSMutableArray alloc]initWithArray:arrayResult];//carshing here now
更新:
- (void)saveNearMeData:(id)response
{
NSArray *finalarray1 = [[NSMutableArray alloc]init];
NSMutableArray = [response valueForKey:@"Deals"];
indexpathArray = [[NSMutableArray alloc]initWithArray:finalarray1];
NSLog(@" finalarray before ::: %@",finalarray1);
if(responseJson.count>0){
NSMutableArray *arrayResult = [[NSMutableArray alloc]initWithArray:responseJson];;
// Now add objects top arrayResult
[arrayResult addObject:finalarray];
NSLog(@" arrayResult before ::: %@",arrayResult);
responseJson = arrayResult;
}else{
responseJson = [response valueForKey:@"Deals"];
}
}
最佳答案
请在解析时分配数据时分配您的数组。希望这能解决您的崩溃问题。
objective-C
if ([arrayList count] == 0) {
arrayList = [[NSMutableArray alloc] initWithArray:[response valueForKey:@"Deals"]];
} else {
[arrayList addObjectsFromArray:[response valueForKey:@"Deals"]];
}
在 Swift 3.0 中
if arrayList.count == 0 {
arrayList = response.value(forKey: "Deals") as! Array
} else {
arrayList += response.value(forKey: "Deals") as! Array
}
关于ios - append 两个数组给出 [__NSCFArray insertObject :atIndex:]: mutating method sent to immutable object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870778/