NSNumber intValue giving EXC_BAD_ACCESS
I have an NSNumber being passed to be via a 3rd party API, and when I call intValue on the index I get a EXC_BAD_ACCESS开发者_如何转开发 error:
-(CPFill *) barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSNumber *)index;
{
NSLog(@"bar index %i", index);
int value = [index intValue];
}
The output I get in the debugger is:
bar index 0
bar index 1
Program received signal: “EXC_BAD_ACCESS”.
What the heck is going on?
I have noticed that the first time the method is called, index is nil but the next time its obviously not...
How can I debug this?!?! Its such a trivial thing, but I cant seem to fix it!
A quick Google turns up a framework that appears to call this method with an NSUInteger
as the argument. Assuming this is the library in question, you're incorrectly typing the argument as an NSNumber*
. When you think you see nil
for the index, you're actually just seeing the index 0.
It would seem either something is wrong with the API, or you've used the wrong method signature. Is index supposed to be a primitive type, like NSInteger?
Try using %@
for printing index
, since index
is an object. Or, %d
with [index intValue]
.
-(CPFill *) barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSNumber *)index
{
NSLog(@"bar index %@", index);
//or NSLog(@"bar index %d", [index intValue]);
int value = [index intValue];
}
Edit: btw, you have a semicolon at the end of your method heading
精彩评论