CorePlot in Swift can not call “numberForPlot”

我怕爱的太早我们不能终老 提交于 2020-02-22 10:57:32

问题


I am using CorePlot(https://github.com/core-plot/core-plot) in Swift. In order to make a pie chart on iOS, I wrote as below by translating Obj-C code to swift. I was able to view the graph title, but I could not plot a pie chart.

As a result of looking at NSlog, I found that “numberForPlot” function was apparently not called.

How should I rewrite this?

My code looks like this: 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;
    }

}

Here's “All Output”.

self.view.addSubview
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot

Would appreciate any help, thanks!

PS

previous Obj-C code

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

And, Here's "numberOfRecordsForPlot" returning.

func numberOfRecordsForPlot(plot:CPTPlot)-> Int {
    println("self.dataForChart.count: \(self.dataForChart.count)");
    return self.dataForChart.count;
}

Output

self.view.addSubview
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4

回答1:


Try declaring the datasource methods like this:

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

The signatures have to match the Objective-C declarations exactly or they won't be called.




回答2:


numberForPlot is defined in the CPTPlotDataSource this way:

-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx;

The return type of id gets translated into Swift as AnyObject! -- if you change your method to this it should get called correctly:

func numberForPlot(plot:CPTPlot, fieldEnum:Int, index:Int) -> AnyObject! {
    println("numberForPlot")
    return self.dataForChart[index] as NSNumber
}


来源:https://stackoverflow.com/questions/24806059/coreplot-in-swift-can-not-call-numberforplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!