通常,當我創建一個對象並將其分配給一個實例變量時,我分配一個臨時對象,調用iVar setter來保留該對象,然後釋放該臨時對象。然而,我在今天早上看到了init,並注意到如果我直接分配iVar,則它保留在alloc中,同時在調用setter或執行dealloc時也可以正確釋放。我只是好奇,如果我正確理解這一點?內存管理和實例變量?
@property(nonatomic, retain) CLLocationManager *locationManager;
。
@synthesize locationManager;
// VERSION 001
- (id)init {
self = [super init];
if(self) {
CLLocationManager *tempManager = [[CLLocationManager alloc] init];
[self setLocationManager:tempManager];
[tempManager release];
}
return self;
}
// VERSION 002
- (id)init {
self = [super init];
if(self) {
locationManager = [[CLLocationManager alloc] init];
}
return self;
}
- (void)dealloc {
[locationManager release];
[super dealloc];
}
謝謝你一個非常明確的答案,非常感謝。 – fuzzygoat 2010-11-25 16:24:58