jinglesthula
jinglesthula

Reputation: 4577

NSDictionary and EXC_BAD_ACCESS

Tried to find the answer here and eventually found a clue on another site. Posting here in case anyone searches here and has the same problem.

NSDictionary *d = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"foo", YES, 42, nil] 
forKeys:[NSArray arrayWithObjects:@"bar", @"baz", @"count", nil]];

This produces:

Program received signal: "EXC_BAD_ACCESS"

What is the cause of this?

Upvotes: 4

Views: 1741

Answers (3)

Sam Baumgarten
Sam Baumgarten

Reputation: 2249

A int nor a BOOL are objects and therefore cannot be part of a NSDictionary. Instead of using int's and bools use their object equivalent of a NSNumber. You can easily store a int using:

[NSNumber numberWithInt:(int)]

You can store a BOOL with:

[NSNumber numberWithBool:(BOOL)]

Upvotes: 0

Myron Slaw
Myron Slaw

Reputation: 886

For one thing, in your array, YES and 42 are not objects. Try using [NSNumber numberWithInt:42] there. You should have got a compiler warning there.

Upvotes: 1

Caleb
Caleb

Reputation: 125007

YES and 42 are not object pointers. You're trying to create an NSArray, which can only contain objects, and you're passing in values that are not pointers to objects. You'll crash for the same reason that

[YES description];

will crash -- YES is not a valid object pointer.

Upvotes: 5

Related Questions