I am drawing pie-charts using HighChart Library (Pie-chart). While Passing data to chart my code is like this :
pie.data = [NSMutableArray arrayWithObjects:@{
@"name": @"New Jersey",
@"y": self->numPlusNJ
},
@{
@"name": @"Miami FLG",
@"y": self->numPlusMiamiFLG
},
@{
@"name": @"Chicago",
@"y": self->numPlusChicago
},nil];
where the values like self->numPlusNJ are NSNumbers. Sometimes after calculations value like 0(zero) comes in NSNumbers and it is taking those values as NAN. In those cases I am getting this error while drawing pie-chart :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid number value (NaN) in JSON write'.
I tried to Replace NAN value by 0 like this :
if(isnan([self->numRPercentNJ doubleValue])){self->numRPercentNJ = 0;}
But in that case I am getting can't insert nil in dictionary object.
So, Currently I am unsure about how to replace this NAN value by something in order to avoid crash.
Assuming your numbers (numPlusMiamiFLG
, etc.) are properties on your current class, I'd suggest implementing an accessor for each one:
- (NSNumber *)numPlusMiamiFLG
{
// Check if NaN
if ([[NSDecimalNumber notANumber] isEqualToNumber: _numPlusMiamiFLG] {
return [NSNumber numberWithDouble:0]; // Or whatever default
} else {
return _numPlusMiamiFLG;
}
}
There's a chance you are not initializing your numbers correctly if you are getting nil
/NaN
. So I would double-check them.
Also note that the ->
is not supposed to be used on Objective-C classes. As seen in this SO question.