I have read Apple's official document, "Advanced Memory Management Programming," and have never understood why the accessor method cannot be used in the Init and Dealloc methods. His usual work in practice, seemingly did not find any problems. Until today, when debugging a mistake, I suddenly understood the reason.
It is not possible to use accessor in init and dealloc because we have to beware: subclasses may overwrite the accessor method of the parent class and change the behavior of the accessor method.
Reference code:
#import <Foundation/Foundation.h> @interface Test:nsobject@property (nonatomic, retain) NSString * str;@ End@implementation test-(ID) init{self = [super init]; if (self) { self.str = @ "AAA"; } return self;} @end @interface test1:test@end@implementation test1-(void) Setstr: (NSString *) str{ [Super setstr: [NSString stringwithformat:@ "[%@]", str]];} @endint Main () { @autoreleasepool { Test *test = [[test new] autorelease]; NSLog (@ "test=%@", test.str); Test1 *test1 = [[Test1 new] autorelease]; NSLog (@ "test1=%@", test1.str); return 0;} }
Output Result:
TEST=AAATEST1=[AAA]