Silver Challenge


#1

I think I’m on the boarder between understanding and not understanding what I’m doing and I want to be sure before I move on. What I have seems to be working but when I read the posts on this challenge I start getting confused. I’m going to post what I have and hopefully some one can tell me whether I’m on the right track. Thanks in advance…

BNRItem.h

[code].
.
.

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

  • (id)initWithItemNameSilverChallenge:(NSString *)name
    serialNumber:(NSString *)sNumber;[/code]

BNRItem.m
In my + (id)randomItem I keep the

int randomValue = rand() % 100;And have the warning of an unused variable.

Should I replace

BNRItem *newItem = [[self alloc] initWithItemNameSilverChallenge:randomName serialNumber:randomSerialNumber];with

BNRItem *newItem = [[self alloc] initWithItemNameSilverChallenge:randomName valueInDollars:randomValue serialNumber:randomSerialNumber];

[code]- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass’ designated installer
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 initiated object
return self;

}

  • (id)initWithItemNameSilverChallenge:(NSString *)name
    serialNumber:(NSString *)sNumber
    {
    return [self initWithItemName:name valueInDollars:0 serialNumber:sNumber ];
    }
  • (id)randomItem
    {
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
    @“Rusty”,
    @“Shiny”, nil];

    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”,
    @“Spork”,
    @“Mac”,nil];

    // Get the index of random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to inclusive.
    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];

    // Note that NSInteger is not an object, but a type definition
    // for “unsigned long”

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    [randomAdjectiveList objectAtIndex:adjectiveIndex],
    [randomNounList objectAtIndex:nounIndex]];

    int randomValue = rand() % 100;

    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10];

    BNRItem *newItem = [[self alloc] initWithItemNameSilverChallenge:randomName
    serialNumber:randomSerialNumber];
    return newItem;
    }[/code]


#2

I think the purpose of the silver challenge is to create an alternative/extra initializer on top of the designated initializer that was created in the chapter. This meaning the following code should still be in your BNRitem.m

BNRItem *newItem = [[self alloc] initWithItemNameSilverChallenge:randomName valueInDollars:randomValue serialNumber:randomSerialNumber];

This will solve your ‘unused variable’ warning because in your class method + (id) randomItem it will still call the above piece of code.


#3

Thanks! Shortly after my post I went back and added that and it fixed everything. I think I sometimes tend to over think through exercises such as this. :wink: