fi12
fi12

Reputation: 485

How can I implement NSNumber values instead of NSDecimal values?

I'm using the latest version of CorePlot, and I'm running into this error:

Sending 'NSDecimal' to parameter of incompatible type 'NSNumber * _Nonnull'

I realize that this error occurs because I'm basing my app on a tutorial that uses an older version of CorePlot. This changelog states one of the changes:

Changed all public properties and methods that take NSDecimal values to take NSNumber values instead. In some cases, the NSDecimal version of the method remains and a new version that takes NSNumber values was added.

Swift does not support NSDecimal values. Using NSNumber values instead has several advantages. Swift automatically bridges numeric values to NSNumber as needed. This also reduces the need to use the NSDecimal wrapper functions, e.g., CPTDecimalFromDouble() to convert values in Objective-C. When greater precision is required for a particular value, use NSDecimalNumber to maintain full decimal precision.

Thus, I know what my error is caused by but I don't know how to fix it. Any ideas? Here's my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CPTGraphHostingView* hostView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview: hostView];

    CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
    hostView.hostedGraph = graph;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;

    [plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( 16 )]];
    [plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( -4 ) length:CPTDecimalFromFloat( 8 )]];

    CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];

    plot.dataSource = self;

    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
}

Upvotes: 2

Views: 374

Answers (1)

NobodyNada
NobodyNada

Reputation: 7634

Use NSNumber objects instead of NSDecimals:

[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:@0 length:@16]];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:@-4 length:@8]];

If you ever need to convert an NSDecimal to an NSNumber, you can use NSDecimalNumber:

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithDecimal:someDecimal];

Upvotes: 2

Related Questions