Swift中的CorePlot无法调用“numberForPlot”

时间:2014-07-17 14:16:35

标签: ios objective-c swift core-plot

我在Swift中使用CorePlot(https://github.com/core-plot/core-plot)。 为了在iOS上制作饼图,我通过将Obj-C代码转换为swift来编写如下。 我能够查看图表标题,但我无法绘制饼图。

通过查看NSlog,我发现“numberForPlot”功能显然没有被调用。

我该如何重写?

我的代码如下所示: ViewController.swift

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

2 个答案:

答案 0 :(得分:1)

尝试声明这样的数据源方法:

func numberOfRecordsForPlot(plot: CPTPlot!) -> UInt {}
func numberForPlot(plot: CPTPlot!, field: UInt, recordIndex: UInt ) -> AnyObject! {}

签名必须与Objective-C声明完全匹配,否则不会被调用。

答案 1 :(得分:0)

numberForPlotCPTPlotDataSource这种方式定义:

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