2017-02-07 2 views
2

Ich verwende die neueste Version von CorePlot, und ich bin in diesen Fehler ausgeführt wird:Wie kann ich NSNumber-Werte anstelle von NSDecimal-Werten implementieren?

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

Ich weiß, dass dieser Fehler auftritt, weil ich auf einem Tutorial meine app bin stützen, die eine ältere Version verwendet von Kernplot. This changelog Staaten eine der Änderungen:

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.

So, ich weiß, was mein Fehler verursacht wird, aber ich weiß nicht, wie es zu beheben. Irgendwelche Ideen? Hier ist mein 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]; 
} 

Antwort

2

Verwenden NSNumber Objekte anstelle von NSDecimals:

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

Wenn Sie jemals eine NSDecimal zu einem NSNumber konvertieren müssen, können Sie NSDecimalNumber:

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithDecimal:someDecimal]; 
+0

Dies ist genau das was ich gesucht habe! – fi12

Verwandte Themen