Objective-Cでinitまたはdeallocメッソッドにaccessorを使用しないサンプル

原因:

サブクラスは親クラスのaccessorメソッドをオーバーライドして、accessorメソッドを変更する可能性がある

コード下記:

#import <Foundation/Foundation.h>

@interface Test : NSObject
@property (nonatomic, retain) NSString * str;
@end

@implementation Test
– (id)init
{
self = [super init];
if(self){
self.str = @"startnews24″;
}
return self;
}
@end

@interface Test1 : Test
@end

@implementation Test1
– (void)setStr:(NSString *)str
{
[super setStr: [NSString stringWithFormat:@"[%@]", str]];
}
@end

int main()
{
@autoreleasepool{
Test *test = [[Test new] autorelease];
NSLog(@"test=%@", test.str);

Test1 *test1 = [[Test1 new] autorelease];
NSLog(@"test1=%@", test1.str);

return 0;
}
}

結果:
test=startnews24
test1=[startnews24]

IOS

Posted by arkgame