Trouble with Silver Challenge


#1

Hi there,

I’m having problem with the output of the silver challenge, it kind of works but it also spits out the worth and date as follows:

"2013-05-29 16:58:15.163 RandomPossesions[1897:303] Andrew's Sofa (ABCD1234): Worth $0, recorded on (null)"
here is my BNRItem.h

// SilverChallenge -(id)initWithItemNameSilver: (NSString *)SilverName serialNumber:(NSString *)SilverNumber;

here is my BNRItem.m

[code]// Silver Challenge

  • (id)initWithItemNameSilver: (NSString *)SilverName
    serialNumber:(NSString *)SilverNumber{

    self = [super init];

    if (self) {
    [self setItemName:SilverName];
    [self setSerialNumber:SilverNumber];
    }
    return self;
    }
    [/code]
    now it supers init that has…-(id)init {
    return [self initWithItemName:@“Item” valueInDollars:0 serialNumber:@" "];
    }

and here is my main.m

[code]// Silver Challenge
BNRItem *Silver = [[BNRItem alloc] initWithItemNameSilver:@“Andrew’s Sofa” serialNumber:@“ABCD1234”];
NSLog(@"%@", Silver);

[/code]

I don’t know how this where the problem is… any suggestions?


#2

Hello,

When you print out an object with NSLog() the object’s “description” method will be invoked and return a string of relevant info. See pages 46/47 of the book.
In your case, assuming you have the same “description” method in BNRItem.m, “dateCreated” is null. You’ll need to set it or figure out why it isn’t being set.

On another note, the normal convention for Obj-C and virtually every other object languages (though I can’t seem to find this explained in chapter 2) is to name variables starting with lower case letters and to name classes starting with upper case letters. The convention will help with readability later on.

// SilverName and SilverNumber should start with lower case letters
-(id)initWithItemNameSilver: (NSString *)SilverName
         serialNumber:(NSString *)SilverNumber

// Code like this can be confusing
// it implies you are passing a class named SilverName setItemName method
if (self) {
        [self setItemName:SilverName];
        [self setSerialNumber:SilverNumber];
    }

Hope this helps.