views:

136

answers:

3

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!

+1  A: 

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

Kurbz
%d for [index intValue]
falconcreek
A semicolon is allowed before the method body, presumably to allow copying and pasting from headers.
Chuck
Ah, didn't know that.
Kurbz
Thanks Kurbz, stupid copy and paste...
Mark
+2  A: 

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?

Brian
+4  A: 

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.

Chuck
yes, nice work Chuck, I copied and pasted the code i was using from a sample implementation or core-plot, which I did not test (assumed it worked)
Mark