Silver Challenge


#1

Hello, here is my solution for the Silver Challenge with the additional initializer.
If someone spots a mistake or has another one that is a more efficient solution, please share or correct me!

.h file
-(instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber;
.m file
-(instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
{
    self = [super init];
    
    if (self)
    {
        _itemName = name;
        _serialNumber = sNumber;
    }
    return self;
}

#2

Don’t do the initialization here. Call the designated initializer:

- (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
{
    return [self initWithItemName:name
                   valueInDollars:0
                     serialNumber:sNumber];
}

#3

[quote=“jcheze”]Don’t do the initialization here. Call the designated initializer:

[code]

  • (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
    {
    return [self initWithItemName:name
    valueInDollars:0
    serialNumber:sNumber];
    }
    [/code][/quote]

Thanks for that!


#4

Finally you can test this code with:

//Silver
BNRItem *silver = [[BNRItem alloc]initWithItemName:@"Silver" serialNumber:@"ABC"];
NSLog(@"%@", silver);