I've read through many SO posts before asking this question and I'm guessing this answer was in there somewhere, but I didn't see it. I'm new to Objective-C and I'm trying to do a (seemingly) simple action that I can't figure out.
The general idea is that I have an NSArray
filled with objects (specifically of type UIImageView
). I want to copy that array, which I've done a number of ways (all successful).
After a copy it, I now have two arrays. I want to modify an object, say at index 2, ONLY in the copy.
So far it seems like, because the copy is merely copying the reference (or pointer), changing the object at index 2 will change it in both the copy and the original.
Does that make sense?
NSArray *originalArray = @[object1, object2];
Ways I've tried copying this array, so that I can achieve what I want:
NSMutableArray *originalArrayCopy = [NSMutableArray arrayWithArray:originalArray];
NSArray *originalArrayCopy = [originalArray copy];
NSMutableArray *originalArrayCopy = [[NSMutableArray alloc] initWithArray:originalArray];
And it seems that in each case, modifying an object from the copy also modifies it in the original.
NOTE: While NSArray
is obviously immutable, the objects within my original array are mutable.