I have two different NSArray objects, lets say –
NSArray* a and NSArray* b
Array b is used to store textual content from csv file.
Now. if I do an operation like the following:
a = b;
Am I copying the content of b to a or am I making a point to b? In the former case, changes made to array a won’t be reflected in array b. On the other hand, in the latter case, they will be.
Thanks
Just a memory address (reference).
If you are coming from C development, you should know that the star
*near the NSArray type means it is a pointer type. This is the same in Objective-C.(If you don’t know C and this stuff about pointers, just forget it, as every object type in ObjC is a pointer, it is quite transparent to the final coder)
If you want to create an independant copy of
b, so that modifyingadoes not modify b then, use thecopymethod. (If you do, don’t forget to release it later, as anyalloc/retain/copyhave to be balanced by arelease/autoreleasecall at the end)Note anyway that an
NSArrayis immutable by construct, meaning that there is no method in theNSArrayclass to modify its content. So for your exact example, pointing to the sameNSArray(without making a deep copy) is not really a problem as you can’t modify anNSArray.This is starting to become an issue only if you use a
NSMutableArray, that do have methods to modify their contents.When working with mutable classes like
NSMutableArray, you then should usemutableCopyto have a mutable copy (instead ofcopywhich returns an immutable object)Let me illustrate this in the following example: