开发者

iOS - assignment of ivars through properties

开发者 https://www.devze.com 2023-03-22 06:59 出处:网络
Given this instance variable: UILabel *label; And the below property: @property (nonatomic,retain) UILabel *label;

Given this instance variable:

UILabel *label;

And the below property:

@property (nonatomic,retain) UILabel *label;

And the following synthesize:

@synthesize label;

Are these below assignments correct (with respect to correct memory management):

// 1
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGSizeZero];
self.label = tmpLabel;
[tmpLabel release];

// 2
self.label = [[[UILabel alloc] initWithFrame:CG开发者_运维技巧SizeZero] autorelease];

// 3 - This one looks shady but I haven't gotten any leaks ( I suppose this will
// not use the created setter)
label = [[UILabel alloc] initWithFrame:CGSizeZero];

- (void)viewDidUnload {
    self.label = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [label release];
    [super dealloc];
}


All are correct. The only thing to note is that if you release label in -viewDidUnload you will have to recreate it in viewWillLoad. You are also correct in that number three will not go through the setter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号