Is This Silver Challenge Solution Correct?


#1

Can someone tell me if this is correct for the Chapter 2 Silver Challenge?

- (id)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber { [self setItemName:name]; [self setSerialNumber:sNumber]; return self; }


#2

Probably sth like this is what you wanted:

- (id)initWithItemName:(NSString *)name
             serialNumber:(NSString *)sNumber
{
    if (self = [super init...])
    {
        [self setItemName:name];
        [self setSerialNumber:sNumber];
    }
    return self;
}

Or like this:

- (id)initWithItemName:(NSString *)name
             serialNumber:(NSString *)sNumber
{
    return [self initWith...];
}

Because initWithItemName:serialNumber: looks like an initialiser, and all initialisers should call other initilialsers either via super or self.


#3

Tom: Your solution is not correct. Your new initializer should call the designated initializer initWithItemName:valueInDollars:serialNumber.


#4

Thanks for the response. Actually re-reading this section again and chapter 29 from Objective-C Programming and I think it’s making much more sense now.