【问题标题】:iOS - assignment of ivars through propertiesiOS - 通过属性分配 ivars
【发布时间】:2011-07-21 23:25:01
【问题描述】:

给定这个实例变量:

UILabel *label;

还有以下属性:

@property (nonatomic,retain) UILabel *label;

然后合成如下:

@synthesize label;

以下这些分配是否正确(关于正确的内存管理):

// 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];
}

【问题讨论】:

    标签: ios memory-management properties instance-variables


    【解决方案1】:

    一切都是正确的。唯一需要注意的是,如果您在 -viewDidUnload 中释放标签,则必须在 viewWillLoad 中重新创建它。你也是正确的,第三个不会通过二传手。

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      相关资源
      最近更新 更多