ajnet
ajnet

Reputation: 27

NSMutableArray EXC_BAD_ACCESS (code=1)

I have no idea why, but my NSMutableArray 'items' will not take more than 5 elements.

Can someone please help? I'm following the Big Nerd Ranch iOS Programming book.

This code works fine:

NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 5; i < 10; i++) {
    BNRItem *p = [BNRItem randomItem];
    [items addObject:p];
}

However if I change the initial value of i to 4 or less the program crashes when exiting the for loop:

NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 4; i < 10; i++) {
    BNRItem *p = [BNRItem randomItem];
    [items addObject:p];
}

Error screenshot: http://db.tt/3CdueSYh

Upvotes: 2

Views: 5915

Answers (2)

mttrb
mttrb

Reputation: 8345

In the screenshot you posted in your comments you are adding a C string, "Mac", to your randomNounList array. You need to make this an NSString with an @ symbol.

I suspect the crash is occurring when this entry is randomly selected.

I'm surprised this compiled, I suspect you are ignoring some warnings.

Upvotes: 3

Novarg
Novarg

Reputation: 7440

Change your

NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", "Mac", nil];

to:

NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", @"Mac", nil];

You forgot @ before "Mac"

Hope it helps

Upvotes: 7

Related Questions