Voldemort Voldemort - 1 year ago 44
iOS Question

Is setting a retained property to nil enough?

I see some code with

@property (nonatomic, readwrite, retain) id something;

And they synthesise it:

@synthesize something = something_;

And in the constructor:

self.something = @"HELLO!";

I assume that, the above line effectively retains that string.

But then, in their dealloc method, they do this:

[self setSomething:nil];

I guess that it is fine, because I imagine that when you set a property to nil, the old value is released. But then, I noticed that all the other classes they did had something like

[something release];

Instead, so I'm no longer sure. Are both ways correct?

Answer Source

Better to use [something_ release]. This won't cause setter to be called, which otherwise could cause some actions to be performed that are undesired in dealloc.