0

Hi I am using below line of code,where both of my responseJson and indexpathArray are NSmutableArrays

[responseJson addObject:indexpathArray];

but I m getting

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

I have referred related topics Getting "mutating method sent to immutable object" error but still the same

Actually here I am trying to append records from new array to old array where I am Getting 10 records every time from server.

Any help is appreciated!

update

    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

update:

 - (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"];
    }
  }
Community
  • 1
  • 1
soumya
  • 3,801
  • 9
  • 35
  • 69

6 Answers6

4

Please alloc your array at the time of assigning data while parsing. Hope that will resolve your crashing issue.

Objective-C

if ([arrayList count] == 0) {
                arrayList = [[NSMutableArray alloc] initWithArray:[response valueForKey:@"Deals"]];
} else {
        [arrayList addObjectsFromArray:[response valueForKey:@"Deals"]];
}

In Swift 3.0

if arrayList.count == 0 {
     arrayList = response.value(forKey: "Deals") as! Array
} else {
     arrayList += response.value(forKey: "Deals") as! Array
}
Maulik Rajani
  • 639
  • 8
  • 17
2

From the log it seems that responseJson is an NSArray and addObject is an NSMutableArray method.

You can do the following

NSMutableArray *arrayResult = [NSMutableArray arrayWithArray:responseJson];
// Now add objects top arrayResult
[arrayResult addObject:indexpathArray];

You are again trying to init the responseJson with the Given array. I think the responseJson is an id, so casting it might solve the problem. Please post the declaration of the responseJson or the method where it is obtained. A safe bet would be to work with the arrayResult rather than the responseJson wherever you need it, and if you need it in any other method then please make arrayResult as an instance variable to obtain it globally.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

You should use addObjectsFromArray method for example :

 NSArray *myArray = ...;
 NSMutableArray *bufferArray = [NSMutableArray new];
 [bufferArray addObjectsFromArray:myArray];
Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
Marat Ibragimov
  • 1,076
  • 7
  • 7
0

Apply deep copy every time you get the response from the server because when you get the response from the server end, generally it is immutable.

Below line do the mutability on each level of responseJson.

NSMutableArray *responseJson = (NSMutableArray
*)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)responseJson, kCFPropertyListMutableContainers));

You can also use the same line for NSMutableDictionary when you get the response in terms of the dictionary.

Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

Try this Solution:

I already have one array newsFeedListArry with objects & I am appending second array.

NSArray *arry = [NSArray arrayWithObject:@"TEstig"];
[newsFeedListArry addObjectsFromArray:arry];
Hemali Luhar
  • 349
  • 1
  • 11
-1

What you can do is

NSMutableArray *test = [NSMutableArray arrayWithArray : responseJson];
[test addObjectsFromArray : indexpathArray];
Avinash Sharma
  • 111
  • 1
  • 11