Supratik Majumdar Supratik Majumdar - 19 days ago 6
Objective-C Question

Call Getter Function Objective-C in Swift (PNChart)

I am using a Framework that is currently in Objective-C in Swift using a Bridging Header

Framework: PNChart

I am not able to call the getter Function in Swift for PNLineChart:
Objective C Code:

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

Answer

Make sure you import the necessary Header files in your bridging header:

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

Then you can assign the function to getData() like this:

   // 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)
    }