My attempt at the Bronze Challenge


#1

I’m new to programming,
but this is what I did for this challenge:

[code] @autoreleasepool {

    // Create a mutable array object, store its address in items variable
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        BNRItem *item = [BNRItem randomItem];
        [items addObject:item];
    }
    
    for (BNRItem *item in items) {
        NSLog(@"%@", item);
    }
    
    id eleventh = [items objectAtIndex:10];
    
    [eleventh count];
    
    // Destroy the mutable array object
    items = nil;
    
}
return 0;

}[/code]

And here is the exception that was given:

[quote]*** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 … 9]’
***[/quote]

…I don’t know how to ask but, is this the result I’m looking for?..


#2

Yep, that’s exactly it.
Ultimately, the important take-away here is the reason given by the exception, which helpfully explains that you’ve asked for index 10 but only 0 … 9 are valid.


#3

Thanks!

Its a good thing that I did it because
I had some pop up when I was working
on the gold challenge :slight_smile:


#4

You should try to get away from using ObjectAtIndex…

Here is another way :

        //Bronze
        
        NSString *eleventh = items[10];
        NSLog(@"%@", eleventh);