I am trying to use the addObjectsFromArray to “concatenate” (so to speak) two arrays together. Here is an overview of the code:
NSArray *fixed = [NSArray arrayWithArray:timeline2];
NSLog(@"Made the temp successfully.Contents: %@", fixed);
[arrayOfNewsItems addObjectsFromArray:fixed];
The Log statement can confirm that there is valid data in the “fixed” and “timeline2” arrays, and earlier in the program, I confirmed that data exists in the “arrayOfNewsItems” array.
The exception I receive after the attempt at addObjects.. is this:
'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
arrayOfNewsItems is a NSMutableArray. It’s a property. Actually, it’s declared like this:
@property (nonatomic) NSMutableArray * arrayOfNewsItems;
Perhaps it’s the “nonatomic” statement? I don’t fully understand these either.
The only thing I can think of is that the array elements in each array have different object types – or rather, although they are each arrays of dictionary objects, the objects themselves consist of a different list of key/value pairs. For example:
Array element 1 might contain:
(key) (value)
name Joe
title Mr.
And Array element 2 might contain:
(key) (value)
phone 555-1263
shoeSize 2
Now, in any other language, I would never try this, but I’m pretty sure I was told in an earlier post that this was OK to do. I understand that objective-c stores them as (id), and we can mash everything in there together like that. So, the questions are:
- Is this true?
- If so, what have I missed?
I would to get a solid answer on this, before I start coding a function to make all field names the same for the final array.
I would certainly appreciate any and all help I can get! Thanks.
Well, it’s pretty clear:
mutating method sent to immutable objectThis means that you have an immutable instance of
NSArray.Even though it is defined as
NSMutableArray, it doesn’t mean that it really is one. It only removes the warnings when trying to call mutating methods like-addObjectsFromArray:.You have to make sure that when creating the array, you send alloc/init to the mutable version: