Ram
Ram

Reputation: 2513

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

Upvotes: 1

Views: 102

Answers (1)

Eiko
Eiko

Reputation: 25632

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.

Upvotes: 2

Related Questions