2014-07-17 20 views
2

我在Swift中使用CorePlot(https://github.com/core-plot/core-plot)。 爲了在iOS上製作餅圖,我通過將Obj-C代碼翻譯爲swift來編寫如下。 我能夠查看圖表標題,但無法繪製餅圖。Swift中的CorePlot無法調用「numberForPlot」

作爲查看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 
+0

如果您以前的Obj-C代碼工作,您可以發佈'-viewDidLoad'方法嗎? – Undo

+1

什麼是numberOfRecordsForPlot返回?如果它是0,那麼numberForPlot可能不是故意調用的。 –

回答

0

numberForPlotCPTPlotDataSource這樣定義的:

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

id返回類型被轉換爲斯威夫特AnyObject! - 如果你改變你的方法這應該被調用正確:

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

謝謝你回答我的問題。我改變了,但「numberForPlot」功能尚未被調用... – ksmzn

1

嘗試宣告數據源的方法是這樣的:

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

的簽名必須完全匹配Objective-C的聲明,否則將不會被調用。

相關問題