我有一个部分基类rnchartbaseview(协议和扩展),以及诸如rnlinechartview,rnbarchartview等的一堆子类等。
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
但我得到
[reactNativeCharts.RNLineChartView setData:]: unrecognized selector sent to instance 0x7fdf60e04510'
如果我将setData从扩展名复制到rnlinechartview.swift,则每件事都很好。
问题在哪里?
更新
似乎是必需的。
但是以下代码不是优雅的,如何以正确的方式写入?
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;
}
}
更新
终于使用了扩展和子类的组合。
模板代码正在扩展。在rnchartbaseviewClass
中class RNChartViewBaseClass: UIView, RNChartViewBase {
......
func setData(_ data: NSDictionary) {
(self as RNChartViewBase).setData(data)
}
}
问题在于,Objective-C在React-Native使用Objection-C的消息传递机制(OBJC_MSGSEND)的同时,对协议扩展一无所知。您不能使用协议扩展来注入一种方法,以便对Objective-C的消息传递机制可见。
而不是协议,您可以在那里创建一个Uiview的子类,并在此处声明SetData方法。