2011-07-21 33 views
0

高德的分配鑑於這個實例變量:iOS版 - 通過屬性

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

回答

0

所有的都是正確的。唯一需要注意的是,如果您在-viewDidUnload中釋放標籤,則必須在viewWillLoad中重新創建標籤。你也是正確的,第三名不會通過二傳手。