Bronze Challenge: Bug Finding


#1

This was my solution for the challenge:

[code]NSString *bug = [NSString stringWithFormat:@"%@", [randomAdjectiveList objectAtIndex:12]];

NSLog(@"%@", bug);[/code]

I think it worked. This was the output:

2012-04-03 17:52:45.694 RandomPossessions[9162:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 12 beyond bounds [0 .. 2]' *** First throw call stack: ( 0 CoreFoundation 0x00007fff85ae6fc6 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff89fbad5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff85a90660 -[__NSArrayI objectAtIndex:] + 208 3 RandomPossessions 0x00000001000018fd +[BNRItem randomItem] + 1133 4 RandomPossessions 0x0000000100000db7 main + 135 5 RandomPossessions 0x0000000100000d24 start + 52 ) terminate called throwing an exception

Please tell me if I did this correctly. Thank you!


#2

I think the error message your getting is generally pretty much the same, but I took the challenge as intending to mean the array of items that you’re filling up in the main function. Also if you interpreted the instructions literally (i.e. I want exactly the 11th item), you’d want to use 10 as your index. Or you could use a value that will always be “one too many” like this:


#3

How is this?

Exception:


#4

Oh, yes. You are right syates21. Thanks, I did not realize that.


#5

The challenge says to ask for the eleventh item of the array. The first for loop in main.m created an array of ten items:

for (int i = 0; i < 10; i++) { BNRItem *p = [BNRItem randomItem]; [items addObject:p]; }
So the index value of the eleventh item (which doesn’t exist) would be 10 since arrays start with an index of 0. I used this code to ask for the eleventh item of the array:

Which threw the error message:
*** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 … 9]’
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8eafcfc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff91acfd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8ea8a582 -[__NSArrayM objectAtIndex:] + 274
3 RandomPossesions 0x0000000104e2004b main + 683
4 RandomPossesions 0x0000000104e1fd94 start + 52
5 ??? 0x0000000000000001 0x0 + 1
)


#6

For some reason, I didn’t really understand the challenge. Seeing these code snippets and results really helped me to further understand. Thanks to everyone!


#7

[quote=“BryanLuby”]I used this code to ask for the eleventh item of the array:

Which threw the error message:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]' *** First throw call stack: ( 0 CoreFoundation 0x00007fff8eafcfc6 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff91acfd5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff8ea8a582 -[__NSArrayM objectAtIndex:] + 274 3 RandomPossesions 0x0000000104e2004b main + 683 4 RandomPossesions 0x0000000104e1fd94 start + 52 5 ??? 0x0000000000000001 0x0 + 1
)[/quote]

I was proud and surprised to find people suggesting the same thing I came up with, but unfortunately I’m not getting an exception, I get this:

[...] 2012-05-10 00:00:54.477 RandomPossessions[8484:403] Fluffly Pig (8P9B1): Worth $57, recorded on 2012-05-09 22:00:54 +0000 2012-05-10 00:00:54.478 RandomPossessions[8484:403] (null)

Did something change and the exception is now not being thrown, or am I doing something wrong? My code is exactly the same as those posted before:

UPDATE: Problem solved: I entered the error-inducing bit of code after the: items = nil; which effectively nulls out the array so the NSLog isn’t exceeding any bounds, is that correct?


#8

Here’s mine:

Console Output:


#9

You can also do it with a for loop, I believe, to achieve the requirements of the exercise:

[code]#import <Foundation/Foundation.h>
#import “BNRItem.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    for ( int i = 0; i < 10; ++i ) {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
    }
    
    for (int i = 0; i < 11; ++i )
        NSLog(@"%@", [items objectAtIndex:i]);
    
    items = nil;                    
}

return 0;

}
[/code]


#10

I did something similar that gets the same exception, but instead of in NSLog, I put [items objectAtIndex:10]; under the for loop.

for (int i = 0; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[items addObject:p];
[items objectAtIndex:10];

    for (BNRItem *item in items)
        NSLog(@"%@", item);

My question is. When I ask for any number other than 0, it throws an exception, not just eleventh (10). So really, what is the point of this challenge to throw it on eleventh, if every number throws an exception. Am I doing something wrong?