protocol extension doesn't work with RCT_EXPOR

2019-05-29 16:51发布

问题:

I have a partial base class RNChartBaseView (protocol and extension), and bunch of subclass like RNLineChartView, RNBarChartView etc.

RNChartBaseView.swift

 protocol RNChartBaseView {
      associatedtype T: ChartViewBase;
      associatedtype U: ChartDataEntryBase;

      var chart: T { get }

      func createEntries(_ data: NSDictionary) -> [U];

      func setData(_ data: NSDictionary);

 }

 extension RNChartBaseView  {
       func setData(_ data: NSDictionary) {

           let chartData = createEntries(data);

           chart.data = chartData;
       }
 }

RNLineChartView.swift

 class RNLineChartView : UIView, RNChartBaseView{

      let _chart:LineChartView;

      var chart: LineChartView {
         return _chart
      }

      override init(frame: CGRect) {
         self._chart = LineChartView(frame: frame);

         super.init(frame: frame);

         self.addSubview(_chart);
      }

      func createEntries(_ data: NSDictionary) -> [LineDataEntry] {

        // ....... convert to LineDataEntry array.
        return entries;

      }

 }

RNLineChartViewManagerSwift.swift

 @objc(RNLineChartViewManagerSwift)
 class RNLineChartViewManagerSwift: RCTViewManager {
   override func view() -> UIView! {
     let ins = RNLineChartView()
     return ins;
   }       
 }

RNLineChartViewManager.m

 #import "React/RCTViewManager.h"

 @interface RCT_EXTERN_MODULE(RNLineChartViewManagerSwift, RCTViewManager)

 RCT_EXPORT_VIEW_PROPERTY(data, NSDictionary)

 @end

But I get

[reactNativeCharts.RNLineChartView setData:]: unrecognized selector sent to instance 0x7fdf60e04510'

if I copy setData from extension to RNLineChartView.swift, every thing works fine.

where is the problem?

Update

It seems a base class is necessary.

But following code is not elegant, How to write it in a proper way?

RNChartBaseViewClass

class RNChartBaseViewClass: UIView, RNChartBaseView {

    var chart: ChartViewBase {
        fatalError("subclass should override this function.")
    };


    override init(frame: CGRect) {
        fatalError("subclass should override this function.")
    }

    func createEntries(_ data: NSDictionary) -> [BaseChartDataEntry] {
        fatalError("subclass should override this function.") 
    }



    func setData(_ data: NSDictionary) {
        let chartData = createEntries(data);

        chart.data = chartData;
    }
}

RNLineChartView.swift

class RNLineChartView : RNChartBaseViewClass{
      var _chart : LineChartView;
      override var chart: LineChartView {
         return _chart
      }

      override init(frame: CGRect) {
         self._chart = LineChartView(frame: frame);

         super.init(frame: frame);

         self.addSubview(_chart);
      }

      override func createEntries(_ data: NSDictionary) -> [BaseChartDataEntry] { 
           // I have to use [BaseChartDataEntry] here as return type to pass compile,
           //  although it actually is [LineDataEntry],  it may cause further problems, e.g. 
        // ....... convert to LineDataEntry array.
        return entries;

      }
 }

Update

At last , I use a combination of extension and subclass.

the template code is in extension. and In the RNChartBaseViewClass

class RNChartViewBaseClass: UIView, RNChartViewBase {
      ......
      func setData(_ data: NSDictionary) {
          (self as RNChartViewBase).setData(data)
      }
}

回答1:

The problem is that Objective-C knows nothing of protocol extensions while in the same time React-Native is using Objective-C's messaging mechanism(objc_msgSend). You cannot use a protocol extensions to inject a method so that it will be visible to the Objective-C's messaging mechanism.

Instead of protocol you can create a subclass of UIView and declare setData method there.