ios - append 两个数组给出 [__NSCFArray insertObject :atIndex:]: mutating method sent to immutable object'

标签 ios objective-c nsmutablearray append

您好我正在使用下面的代码行,其中我的 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/

相关文章:

ios - 解决有关自定义单元格的编译时错误

objective-c - NSOutlineView 中数组枚举对象的自定义部分

iphone - 具有多个 objectKeys 的 NSMutableArray 中的 NSDictionary 上的 sortUsingComparator

ios - 正则表达式中的多个 ????'s 导致错误

iphone - 测试理解 Objective-C 与指针

ios - 通过 3D Touch 使用压缩预览 View

ios - 从 NSMutableArray 中删除值时崩溃

ios - 带有 TableView 和手势识别器问题的 Swift/UIView

iOS - 如何在自定义单元格中使用继承?

ios - iCarousel 滚动条