Having a hard time figuring this one out…
does addObjectsFromArray, a convenience method inside of NSArray copy everything, or does it keep the ‘otherArray’ parameter where it is in memory and do a LinkedList style point the tail to the ‘otherArray’ kind of move?
Im asking because I have pointers to important objects in some existing-array, but these pointers may point to a different object on an incoming-array.
I re-point my important-object-pointer to some object within the incoming-array, and then call addObjectsFromArray on the existing-array with the incoming-array.
My worry is that my re-pointed important-object-pointers will point to nothing if the incoming-array is actually copied, when ARC decides to nil out the incoming-array.
I think you’re overthinking this. You won’t have a pointer “into” an NSArray. You’ll have a pointer to an object, and the NSArray will also have a pointer to that object. Reassigning your pointer will not change the pointer in the NSArray.
As for copying, NSArray does not copy objects. But as noted above, it does copy the pointer to the object, rather than holding a pointer-to-a-pointer or something weird like that. So in this code:
At the end of running:
bandcwill be hold same NSMutableString — “Cool beans” — and mutating either of them would show up in the other oneThe array holds the same NSMutableString as
bandc— “Cool beans” — and accessing the string there will also show mutations to the stringawill be nil. This didn’t affect any of the other pointers to the string when we reset it, because they point to the string, not the variable.When you
addObjectsFromArray:, it is just as though you iterated over the array and added each individually. The other array could go away at that point and it wouldn’t matter, because your NSArray now has pointers to all the objects.