开发者

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"

Wh开发者_JAVA技巧at is the cause of this?


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.


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.


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)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜