[英]iOS - assignment of ivars through properties
給定這個實例變量:
UILabel *label;
以及以下屬性:
@property (nonatomic,retain) UILabel *label;
以及以下合成:
@synthesize label;
以下這些分配是否正確(關於正確的 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];
}
全部正確。 唯一需要注意的是,如果您在 -viewDidUnload 中釋放 label,則必須在 viewWillLoad 中重新創建它。 你也是正確的,第三個不會通過設置器 go 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.