追加两个数组给出 [__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
Appending two arrays gives [__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
你好我正在使用下面的代码行,其中我的 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"];
}
}
从日志看来,responseJson
是一个 NSArray,addObject
是一个 NSMutableArray 方法。
您可以执行以下操作
NSMutableArray *arrayResult = [NSMutableArray arrayWithArray:responseJson];
// Now add objects top arrayResult
[arrayResult addObject:indexpathArray];
您再次尝试使用 Given 数组初始化 responseJson。我认为 responseJson 是一个 id
,因此转换它可能会解决问题。请post声明responseJson或获取方法。
一个安全的选择是在任何需要的地方使用 arrayResult
而不是 responseJson
,如果您在任何其他方法中需要它,请将 arrayResult
作为实例变量来获取它在全球范围内。
你能做的是
NSMutableArray *test = [NSMutableArray arrayWithArray : responseJson];
[test addObjectsFromArray : indexpathArray];
您应该使用 addObjectsFromArray 方法,例如:
NSArray *myArray = ...;
NSMutableArray *bufferArray = [NSMutableArray new];
[bufferArray addObjectsFromArray:myArray];
每次从服务器端获取响应时应用深拷贝,因为当您从服务器端获取响应时,通常它是不可变的。
下面一行对 responseJson 的每个级别执行可变性。
NSMutableArray *responseJson = (NSMutableArray
*)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)responseJson, kCFPropertyListMutableContainers));
当您根据字典获得响应时,您也可以对 NSMutableDictionary 使用同一行。
请在解析时分配数据时分配您的数组。希望这能解决您的崩溃问题。
Objective-C
if ([arrayList count] == 0) {
arrayList = [[NSMutableArray alloc] initWithArray:[response valueForKey:@"Deals"]];
} else {
[arrayList addObjectsFromArray:[response valueForKey:@"Deals"]];
}
在Swift3.0
if arrayList.count == 0 {
arrayList = response.value(forKey: "Deals") as! Array
} else {
arrayList += response.value(forKey: "Deals") as! Array
}
试试这个解决方案:
我已经有一个包含对象的数组 newsFeedListArry,我正在附加第二个数组。
NSArray *arry = [NSArray arrayWithObject:@"TEstig"];
[newsFeedListArry addObjectsFromArray:arry];
你好我正在使用下面的代码行,其中我的 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"];
}
}
从日志看来,responseJson
是一个 NSArray,addObject
是一个 NSMutableArray 方法。
您可以执行以下操作
NSMutableArray *arrayResult = [NSMutableArray arrayWithArray:responseJson];
// Now add objects top arrayResult
[arrayResult addObject:indexpathArray];
您再次尝试使用 Given 数组初始化 responseJson。我认为 responseJson 是一个 id
,因此转换它可能会解决问题。请post声明responseJson或获取方法。
一个安全的选择是在任何需要的地方使用 arrayResult
而不是 responseJson
,如果您在任何其他方法中需要它,请将 arrayResult
作为实例变量来获取它在全球范围内。
你能做的是
NSMutableArray *test = [NSMutableArray arrayWithArray : responseJson];
[test addObjectsFromArray : indexpathArray];
您应该使用 addObjectsFromArray 方法,例如:
NSArray *myArray = ...;
NSMutableArray *bufferArray = [NSMutableArray new];
[bufferArray addObjectsFromArray:myArray];
每次从服务器端获取响应时应用深拷贝,因为当您从服务器端获取响应时,通常它是不可变的。
下面一行对 responseJson 的每个级别执行可变性。
NSMutableArray *responseJson = (NSMutableArray
*)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)responseJson, kCFPropertyListMutableContainers));
当您根据字典获得响应时,您也可以对 NSMutableDictionary 使用同一行。
请在解析时分配数据时分配您的数组。希望这能解决您的崩溃问题。
Objective-C
if ([arrayList count] == 0) {
arrayList = [[NSMutableArray alloc] initWithArray:[response valueForKey:@"Deals"]];
} else {
[arrayList addObjectsFromArray:[response valueForKey:@"Deals"]];
}
在Swift3.0
if arrayList.count == 0 {
arrayList = response.value(forKey: "Deals") as! Array
} else {
arrayList += response.value(forKey: "Deals") as! Array
}
试试这个解决方案:
我已经有一个包含对象的数组 newsFeedListArry,我正在附加第二个数组。
NSArray *arry = [NSArray arrayWithObject:@"TEstig"];
[newsFeedListArry addObjectsFromArray:arry];