开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜