views:

62

answers:

2

Hey guys,

I noticed that I rarely use properties, due to the fact that I rarely need to access my object's variables outside my class ;)

So I usually do :

NSMutableArray *myArray; // not a property !

My question is : even if i don't declare myArray as a property, does iphone make a retain anyway if I do

myArray = arrayPassedToMe;

I think so but I just wanted to confirm ;)

Any thoughts welcome !

Gotye

+1  A: 

If you do not declare a property with 'retain' then no retain call will be made. It is generally preferable to use the property accessors (for all cases, it makes memory management much simpler), however you can perform a manual retain as such:

myArray = [otherArray retain];
Kevin Sylvestre
So if I understand, if I just domyArray = otherArray;myArray will be a reference to otherArray ? so that if otherArray is released, myArray is automatically released, right ? and if otherArray is modified, myArray would also be modified ?
gotye
Yes, AFAIK that statement simply creates a reference. If you wan't the object to stick around, you should `retain` it.
Rengers
A: 

Just to add to Kevin's answer, in this case you also need to make sure that any existing object at which myArray is currently pointing is freed before you assign new value to it, which means:

[myArray release];
myArray = [otherArray retain];

When you access your class variables via declared properties, all this stuff with retaining/releasing memory is done for you automatically, making your life much easier.

Matthes