ios - Swift 中的 CorePlot 无法调用 “numberForPlot”

标签 ios objective-c swift core-plot

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

通过查看 NSlog,我发现显然没有调用“numberForPlot”函数。

我应该如何重写它?

我的代码如下所示: View Controller .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

非常感谢任何帮助,谢谢!

附言

以前的 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

最佳答案

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

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

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

关于ios - Swift 中的 CorePlot 无法调用 “numberForPlot”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24806059/

相关文章:

ios - 从 Parse 中检索数组并将它们保存在具有相应值的字典中。 [字符串() : [String]()]

ios - 如何从 UICollectionViewFlowLayout 为 itemSize 制作动画?

objective-c - 如何获得有意义的 CIAreaHistogram 输出?

ios - 如何以编程方式创建 UITableView

ios - NSInvalidArgumentException FIRMessaging connectWithCompletion

swift - tvOS隐藏时间栏,同时不丢失音频菜单?

swift - tintedImageProvider 在图形复杂功能中不提供色调颜色

objective-c - 是否有针对 Mac 和 iOS Cocoa/Objective C 的任何商业附加编程控件?

ios - swift : Terminating app due to uncaught exception 'NSInvalidArgumentException' , 原因: 'Entity name must not be nil.'

ios - 如何使用Alamofire上传多张图片?