Silver Challenge Solution - Am I doing right?


#1

Hello there! This is what I’ve done for the Silver Challenge

BNRItem.h

[code]// Designated initilizer which accepts 3 arguments

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNum:(NSString *)sNum;

// Convienience initializer, it takes NSString that identifies
// the itemName and serialNum (silver)

  • (id)initWithItemName:(NSString *)name
    serialNum:(NSString *)sNum;[/code]

BNRItem.m

[code]// Designated initializer

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNum:(NSString *)sNum
    {
    // 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 setSerialNum:sNum];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    }

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

// Another initializer (silver)

  • (id)initWithItemName:(NSString *)name
    serialNum:(NSString *)sNum
    {
    self = [self initWithItemName:name
    valueInDollars:100
    serialNum:sNum];
    return self;
    }[/code]

This is how I called the initializer in main.m

for (int i = 0; i < 3; i++) { BNRItem *b = [[BNRItem alloc] initWithItemName:@"Silver" serialNum:@"S1LV3R"]; NSLog(@"%@", b); }

UPDATE: Here’s the output

2013-08-07 14:52:36.924 RandomPossessionsII[309:303] Silver (S1LV3R): Worth $100, recorded on 2013-08-07 04:52:36 +0000 2013-08-07 14:52:36.924 RandomPossessionsII[309:303] Silver (S1LV3R): Worth $100, recorded on 2013-08-07 04:52:36 +0000 2013-08-07 14:52:36.925 RandomPossessionsII[309:303] Silver (S1LV3R): Worth $100, recorded on 2013-08-07 04:52:36 +0000

I am really looking forward to see the feedback Thank you! :smiley:


#2

This was my solution as well though in the implementation, I have:

return [self initWithItemName: name
                   valueInDollars:0
                     serialNumber: sNumber];

Just saves a few characters.

With regard to how you’re calling it in main.m though, you are creating 3 objects with the same values. You can take your code out of the loop so that it just writes out once with the values you’ve set. If you want to create a loop, I would think you’d have to modify the randomItem class method to call this method (instead of the designated initializer) but I’m not sure about that since I’m just learning myself :slight_smile:


#3

I am not sure (newbie myself) , but I was surprised to see that you set the valueInDollars to 100? Why? Or: why not to 0?


#4

Hi wannanerd,

the reason the valueInDollars instance variable is 100 and not 0 is the previous code BNRItem.m from objcN00b, specifically:

[code]// Another initializer (silver)

  • (id)initWithItemName:(NSString *)name
    serialNum:(NSString *)sNum
    {
    self = [self initWithItemName:name
    valueInDollars:100 <––––––––––––––– The actual instance value (post-initializer)
    serialNum:sNum];
    return self;
    }[/code]
    I wouldn’t include it in the solution having a call method for the new initializer, although it seems reasonable.
    It’s just not asked of in the book, the main reason for the silver challenge was as I see it, just:
    (1) to identify the right initializer (and write it),
    (2) to include a statement inside @interface (BNRItem.h) of the new initializer.