I am a newer to objective c. I have read the memory management document on apple’s “Memory Management Rules”. But I am still not very clear about how to manage reference for a property.
What’s the default implementation of set/get access methods for a property declared with “retain” annotation?
This is my assuming, please give some comments. Thanks.
@interface SubClass : NSObject {
NSString * _name;
}
... ...
@property (nonatomic, retain) NSString * name;
... ...
@end
-(NSString *) setName {
return _name;
}
-(void) setName: (NSString *) pName{
// the correct version of default set method for retain
if( _name != pName ) {
[_name release];
_name = [pName retain];
}
}
So the dealloc method, is this ok?
- (void)dealloc {
self.name = nil; // or [_name release], _name = nil;
}
As Matteo Alessani says, you can simply synthesize the property to get the default implementations.
For reference, this is what’s generated (I got this from reading Objective-C Declared Properties and piecing information together):