如何实现 NSNumber 值而不是 NSDecimal 值?

How can I implement NSNumber values instead of NSDecimal values?

我正在使用最新版本的 CorePlot,我 运行 遇到了这个错误:

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

我意识到发生此错误是因为我的应用程序基于使用旧版本 CorePlot 的教程。 This changelog 说明其中一项更改:

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.

因此,我知道我的错误是由什么引起的,但我不知道如何解决它。有任何想法吗?这是我的代码:

- (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];
}

使用 NSNumber 对象而不是 NSDecimals:

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

如果您需要将 NSDecimal 转换为 NSNumber,您可以使用 NSDecimalNumber:

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithDecimal:someDecimal];