I want to make sure I understand a key concept of properties and instance variable ownership of a class, in this case using NSArray.
Suppose I pass a reference to an NSArray
to another class, and set that class's iVar to it. For example:
First Technique
OtherClass.h
@property (nonatomic, retain) NSArray * otherClassArray;
then:
CurrentClass.m
otherclass.otherClassArray=myArray
Now, even though OtherClass
is retaining myArray
, if CurrentClass
changes myArray
then otherclass.otherClassArray
will also change, correct?
So, is this the better way to do it, or am I mistaken and the above will do what I'd expect from the following anyway:
Second Technique
CurrentClass.m
otherclass.otherClassArray=[NSArray arrayWithArray:myArray]
Now there is a distinct copy being made so any changes to myArray
have no effect on otherClassArray
, am I right, or are both of these approaches doing the same thing?
UPDATE: Is there any reason why my second technique above should or should not be used vs. using copy
with the property? It would seem that either technique results in a new NSArray that my class owns.