Search code examples
objective-cpropertiesretain

Should I use @property (retain) for a @dynamic property setter that is retaining?


I have the following code for a property whose getter/setter I write manually using the @dynamic keyword:

@property (nonatomic, retain) NSObject* obj;

@dynamic obj;
-(NSObject*) obj
{
    return obj;
}
-(void) setObj:(NSObject*)newObj
{
    [obj release];
    obj = [newObj retain];
}

My question is, if I remove the retain from the @property declaration, the compiler complains that the default will be assign and that it may not be what I want. If I add the retain, I assume it is going to be ignored, because I wrote the getters/setters myself?

Just looking for a quick confirmation on this.


Solution

  • You are correct, but your property declaration is as much for documentation as it is for your implementation, at least in this case. At some point, someone (you in six months?) will look at your .h file and say, "Why isn't this value being retained? How does this not crash every time it gets run or leak memory like a sieve?"