Why DON'T we use the property in initializer methods? Use the instance variable
Why DON'T we use the property in initializer methods and to Use the instance variable?
init {
self = [super init];
if (self) {
self.someString = [[[NSString alloc] initWithFormat:@"%@ %@",@”Mike”, @”Jones”] autorelease];
}
return self;
}
vs:
init {
self = [super init];
if (self) {
_someString = [开发者_如何学运维[[NSString alloc] initWithFormat:@"%@ %@",@”Mike”, @”Jones”] autorelease];
}
return self;
}
The correct way is to do
_someString = [[NSString alloc] initWithFormat:@"%@ %@",@”Mike”, @”Jones”];
without the autorelease
. I assume your property to be retain
or (better) copy
.
You don't want to call methods in init and dealloc, as they can easily have side effects, either here (now or later) or in a subclass.
精彩评论