Ios Swift中的CorePlot无法调用“;编号orplot”;
我正在Swift中使用CorePlot()。 为了在iOS上制作饼图,我将Obj-C代码翻译成swift,如下所示。 我可以查看图表标题,但无法绘制饼图 通过查看NSlog,我发现“numberForPlot”函数显然没有被调用 我应该如何重写这个 我的代码如下所示: ViewController.swiftIos Swift中的CorePlot无法调用“;编号orplot”;,ios,objective-c,swift,core-plot,Ios,Objective C,Swift,Core Plot,我正在Swift中使用CorePlot()。 为了在iOS上制作饼图,我将Obj-C代码翻译成swift,如下所示。 我可以查看图表标题,但无法绘制饼图 通过查看NSlog,我发现“numberForPlot”函数显然没有被调用 我应该如何重写这个 我的代码如下所示: ViewController.swift import UIKit class ViewController: UIViewController, CPTPieChartDataSource, CPTPieChartDelega
import UIKit
class ViewController: UIViewController, CPTPieChartDataSource, CPTPieChartDelegate{
@IBOutlet var graphView : CPTGraphHostingView
var dataForChart = NSNumber[]();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var graph = CPTXYGraph(frame:self.graphView.bounds);
self.graphView.hostedGraph = graph;
graph.title = "Graph Title";
graph.axisSet = nil;
var pieChart = CPTPieChart();
pieChart.pieRadius = 80.0;
pieChart.dataSource = self;
pieChart.delegate = self;
graph.addPlot(pieChart);
self.dataForChart = [40, 30, 20, 10];
self.view.addSubview(self.graphView);
println("self.view.addSubview");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfRecordsForPlot(plot:CPTPlot)-> Int {
println("numberOfRecordsForPlot");
return self.dataForChart.count;
}
func numberForPlot(plot:CPTPlot, fieldEnum:Int, index:Int) -> NSNumber {
println("numberForPlot");
return self.dataForChart[index] as NSNumber;
}
}
这里是“所有输出”
self.view.addSubview
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot
非常感谢您的帮助,谢谢
PS
先前的Obj-C代码
- (void)viewDidLoad
{
[super viewDidLoad];
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc]
initWithFrame:CGRectMake(0, 0, 320, 320)];
CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];
hostingView.hostedGraph = graph;
graph.title = @"Graph Title";
graph.axisSet = nil;
CPTPieChart *pieChart = [[CPTPieChart alloc] init];
pieChart.pieRadius = 80.0;
pieChart.dataSource = self;
pieChart.delegate = self;
[graph addPlot:pieChart];
self.pieChartData = [NSMutableArray arrayWithObjects:
[NSNumber numberWithDouble:40.0],
[NSNumber numberWithDouble:30.0],
[NSNumber numberWithDouble:20.0],
[NSNumber numberWithDouble:10.0],
nil];
[self.view addSubview:hostingView];
}
这里是“numberOfRecordsForPlot”返回
func numberOfRecordsForPlot(plot:CPTPlot)-> Int {
println("self.dataForChart.count: \(self.dataForChart.count)");
return self.dataForChart.count;
}
输出
self.view.addSubview
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4
numberForPlot
在CPTPlotDataSource
中定义如下:
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx;
id
的返回类型被转换成Swift作为AnyObject代码>--如果将方法更改为此,则应正确调用该方法:
func numberForPlot(plot:CPTPlot, fieldEnum:Int, index:Int) -> AnyObject! {
println("numberForPlot")
return self.dataForChart[index] as NSNumber
}
尝试如下方式声明数据源方法:
func numberOfRecordsForPlot(plot: CPTPlot!) -> UInt {}
func numberForPlot(plot: CPTPlot!, field: UInt, recordIndex: UInt ) -> AnyObject! {}
签名必须与Objective-C声明完全匹配,否则将不会调用它们。如果您以前的Obj-C代码有效,您可以从中发布-viewDidLoad
方法吗?numberOfRecordsForPlot返回多少?如果是0,则可能不是故意调用numberForPlot。谢谢您回答我的问题。我已经改变了,但是“numberForPlot”函数还没有被调用。。。