Don't use accessor methods during initialization?


#1

According to the Apple Documentation titled "Your Second iOS App: Storyboards)…“An initialization method is one of two places in which you should not use accessor methods to access properties”

However in the Homepwner example we see this:

[code]- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass’s designated initializer
self = [super init];

// Did the superclass's designated initializer succeed?
if(self) {
    // Give the instance variables initial values
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
}

// Return the address of the newly initialized object
return self;

}
[/code]

why not follow this convention…

[code]@implementation BNRItem
@synthesize name = _name, location = _location, date = _date;

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated initializer
    self = [super init];

    // Did the superclass’s designated initializer succeed?
    if(self) {
    // Give the instance variables initial values
    [_name = name];
    [_sNumber = sNumber];
    [_value = value];
    dateCreated = [[NSDate alloc] init];
    }

    // Return the address of the newly initialized object
    return self;
    }
    [/code]

Addionally, according to the Apple Documentation, “properties are usually, but not always, stored as instance variables”. One reason to use IVARS has to do with the type of computer: “a property in a 32-bit process must be backed by an instance variable. In a program with a 64-bit address space, properties do not have to be backed by instance variables.” But I’m sure that the authors of BNR did not use the non-IVAR approach for this reason.


#2

According to a thread on stack overflow, using accessor methods during initialization may cause problems because
"…The object is not fully initialized and the accessor may depend on a fully initialized object."

I’m curious to know what your thoughts on this.

ref: stackoverflow.com/questions/1050 … nd-dealloc