@interface classA:NSObject {} @property (nonatomic,retain) UIImageView* imageView; @end @implementation classA @synthesize imageView; -(void)init { //方法1:使用了get和set方法,但是在实例的时候没有通过self调用,则只增加一次保留记数 imageView = [[UIImageView alloc] init]; NSLog(@"count=%d",[str retainCount]);//count=1 [self addSubview:imageView]; //addSubview方法会增加一次引用记数
NSLog(@"count=%d",[str retainCount]);//count=2 [imageView release]; NSLog(@"count=%d",[str retainCount]);//count=1 //使用此种方法,则需要在dealloc方法中,再次release一次 } @end
@interface classA:NSObject {} @property (nonatomic,reatin) UIImageView* imageView; @end @implementation classA @synthesize imageView; -(void)init { //方法2:使用了get和set方法,在实例的时候通过self调用,则增加二次保留记数,因为在.h中声明了retain self.imageView = [[UIImageView alloc] init];//通过了self.给参数赋值,因为在set的方法实现是先release旧值,再retatin新值,所以alloc了一次,retain新增了一次
NSLog(@"%d",[imageView retainCount]);//count=2 [imageView release]; [self.window addSubview:self.imageView];//addSubview方法会增加一次引用记数 NSLog(@"%d",[imageView retainCount]);//count=2 [imageView release]; NSLog(@"%d",[imageView retainCount]);//count=1
//所以如果在声明中用了retatin,而在实例的时候用了self.的话,需要多release一次,但是如果把@property中的retain改为assign则不会多出一同次count。
} @end
有些东西还是不太理解