我正在使用一个框架,该框架目前在Objective-C中使用Swift的桥接头
框架:PNChart
我无法在Swift中为PNLineChart调用getter函数:
目标C代码:
data01.getData = ^(NSUInteger index) {
CGFloat yValue = [data01Array[index] floatValue];
return [PNLineChartDataItem dataItemWithY:yValue];
};
最佳答案
确保在桥接头中导入必要的头文件:
#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)
}
关于objective-c - 在Swift中调用Getter函数Objective-C(PNChart),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40717648/