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

问题描述:

我使用的是最新版本的CorePlot的,和我遇到了此问题:如何实现NSNumber值而不是NSDecimal值?

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]; 
+0

这是正是我正在寻找的! – fi12