简体   繁体   English

iOS - 通过属性分配 ivars

[英]iOS - assignment of ivars through properties

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):以下这些分配是否正确(关于正确的 memory 管理):

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

// 2
self.label = [[[UILabel alloc] initWithFrame:CGSizeZero] 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.唯一需要注意的是,如果您在 -viewDidUnload 中释放 label,则必须在 viewWillLoad 中重新创建它。 You are also correct in that number three will not go through the setter.你也是正确的,第三个不会通过设置器 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM