在Swift中调用Getter函数Objective-C(PNChart)

时间:2016-11-21 10:13:11

标签: objective-c swift

我正在使用一个框架,该框架目前使用桥接标头在Swift中的Objective-C中

框架:PNChart

我无法在Swift中为 PNLineChart 调用getter函数: 目标C代码:

data01.getData = ^(NSUInteger index) {
CGFloat yValue = [data01Array[index] floatValue];
return [PNLineChartDataItem dataItemWithY:yValue];
};

1 个答案:

答案 0 :(得分:1)

确保在桥接标题中导入必要的标题文件:

#import "PNLineChart.h"
#import "PNLineChartData.h"
#import "PNLineChartDataItem.h"

然后您可以将函数分配给getData(),如下所示:

   // Assuming you have an array of NSNumber's
    let data01Array: [NSNumber] = [1.0, 2.0, 3.0]
    let data = PNLineChartData()

    data.getData = {index in

        // |index| is of type UInt as declared in Objective C Header.
        // CGFloat yValue = [data01Array[index] floatValue];
        let yValue: CGFloat = CGFloat(data01Array[Int(index)])

        // Static function becomes a constructor in Swift 3
        // return [PNLineChartDataItem dataItemWithY:yValue];
        return PNLineChartDataItem(y: yValue)
    }