-1

I have a json parser with AFHTTPRequestOperation.

my parsing code:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

and I have a NSMutableArray

@property (strong, nonatomic) NSMutableArray * myDirectory;

First, I load main 10 items and add with this:

self.myDirectory = responseObject;

After, when I want to load more 10 items, I tried many things but error is:

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

I tried:

[self.myDirectory insertObject:[responseObject mutableCopy] atIndex:[self.myDirectory count]];
[self.myDirectory addObjects:responseObject];
etc...

What I must use?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mess34
  • 1
  • 1
  • 1
  • The error is telling you that the receiver of `insertObject:atIndex` is immutable. Likely, you should be assigning `myDirectory` property differently, e.g. `self.myDirectory = [responseObject mutableCopy]` to insure that it's mutable – FluffulousChimp Apr 20 '14 at 08:56
  • You don't have an NSMutableArray. – Hot Licks Apr 20 '14 at 12:27

3 Answers3

1

AFNetworking returns immutable arrays (NSArray), so you must assign it like:

self.myDirectory = [responseObject mutableCopy]
Matías R
  • 2,195
  • 1
  • 14
  • 12
  • Try replacing the line I put as answer with: self.myDirectory ? [self.myDirectory addObjectsFromArray:responseObject] : self.myDirectory = responseObject; – Matías R Apr 20 '14 at 09:23
  • same error: [__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' – mess34 Apr 20 '14 at 09:32
0

did you try [myArray addObjectsFromArray:otherArray];

Eloy
  • 119
  • 1
  • 9
0

AFNetworking returns immutable arrays. So try this:

self.myDirectory = [responseObject mutableCopy];

When you want to add the second responseObject to previous self.myDirectory try this:

[self.myDirectory addObjectsFromArray: responseObject];

Check this answer for a clear concept about copy and mutableCopy.

Hope this helps.. :)

EDIT:

NSArray *arr = [[NSArray alloc] initWithObjects:@"A",@"B", nil];
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"C",@"D", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"E",@"F", nil];
NSMutableArray *mutableArray= [[NSMutableArray alloc] init];
mutableArray = [arr mutableCopy];
[mutableArray addObjectsFromArray:arr1];
[mutableArray addObjectsFromArray:arr2];
NSLog(@"%@",mutableArray);
Community
  • 1
  • 1
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • I tried, first is removing first items, your second code is not working, same error again. – mess34 Apr 20 '14 at 09:19
  • Try this: [self.myDirectory addObjectsFromArray: [responseObject mutableCopy]]; Let me know. – Rashad Apr 20 '14 at 09:28
  • again error: [__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' – mess34 Apr 20 '14 at 09:30
  • I have many data object type in my application. For example; id, title, author, date, hits, photo, updated_date, etc... should I create initwithObjects for all? – mess34 Apr 20 '14 at 09:59
  • are they array or dictionary? – Rashad Apr 20 '14 at 10:03
  • !!! Its not an array, Its a Dictionary. Thats why you are getting this error. If you share your whole dictionary and tell me what you want then I might help. – Rashad Apr 20 '14 at 10:11
  • sorry but I said first post, its a json. first parsing responseObject can add nsmutabblearray, why second (insert new items) dont work? you are saying its a Dictionary, but I think not. Because AFNetwork AFJSONResponseSerializer converted. Please check my first post again. thanks. – mess34 Apr 20 '14 at 10:16
  • Can you please post you JSON? – Rashad Apr 20 '14 at 10:18
  • [{"id":1,"title":"Title Test","cat":"cat Title","date":"2014"},{"id":2,"title":"Title Test","cat":"cat Title","date":"2014"},{"id":3,"title":"Title Test","cat":"cat Title","date":"2014"},{"id":4,"title":"Title Test","cat":"cat Title","date":"2014"}] – mess34 Apr 20 '14 at 10:21
  • Its an array of NSDictionary. – Rashad Apr 20 '14 at 10:36
  • so, what I should do? – mess34 Apr 20 '14 at 10:38
  • Share your code. Because I think you are doing something wrong in code. The above solution should work. – Rashad Apr 20 '14 at 12:04