views:

187

answers:

3

In my @interface theres a NSArray *Monate followed by:

@property (nonatomic, retain) NSArray* Monate;

If i do:

filePath = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"plist"];
self.Monate = [NSArray  arrayWithContentsOfFile:filePath];

in the constructor, it gets set to an autoreleased object (is that correct?). So should I do a [Monate retain] afterwards?

+2  A: 

You should NOT do a retain after. By setting a @property of retain, some special things happen when you use the self.Monate setter

1) Anything in the Monate instance variable, if any, will get a release.
2) the new assignment will get a retain.

if you were to use @property of assign, then you would have to retain, but you are fine the way you are.

As a side note, in objective-c, Capitalized words are usually reserved for Class names. I sugges changin it to "monate" instead of "Monate" as this could lead to confusion down the road

coneybeare
+4  A: 

This code is correct; you should not add a retain call.

+[NSArray arrayWithContentsOfFile:] will return an autoreleased NSArray. Passing that to -[YourClass setMonate:] will retain the object and assign to the backing ivar. After the constructor returns, the new NSArray will have a retain count of 2 and be added once to the current autorelease pool (resulting in a net retain count of 1)

As long as you release the array in your dealloc, this code is correct.

rpetrich
A: 

[NSArray arrayWithContentsOfFile:]; returns an autoreleased array which will require retaining if you want it to hang around longer than the end of the method.

Notice how your property declaration specifies 'retain'. This means that any self.property = x; calls you do will retain the object you pass in.

So what you are doing there is correct. Just remember to do self.property = nil; in your dealloc method.

Setting a property to nil will release the old object and set the pointer to nil, which is the correct way to do it.

Jasarien