ios - 问题注意 Swift 方法作为 Objective C 中的选择器并在 Swift ios App 中使用 Objective C 对象

标签 ios objective-c swift swift4

我想使用 ANDLineChartView ,一个用 Objective C 编写的 API,用于在我的 Swift 应用程序中将图表创建为 ios 中的 View 。到目前为止,我所尝试的只是将 Objective-C 源代码添加到我的 swift 项目中,并添加一个带有一行代码的桥接头文件 (obj-c):

#import "AndLineChartView.h"

这允许我在我的 swift 代码中创建 ANDLineChartView 类型的对象。问题是 ANDLineChartView 需要一个符合名为 ANDLineChartViewDataSource 的协议(protocol)的数据源,所以我(在 Swift 中)编写了一个符合此协议(protocol)的 @objc 类(并且我扩展了 NSObject,因此它也符合 NSObjectProtocol,这也是必要的) .我在 Swift 中实现了 ANDLineChartViewDataSource 所需的所有必要方法,最后添加:

#import "MyProjectName-Swift.h"

其中MyProjectName当然是我给ANDLineChartView.m的项目名。根据我在互联网上了解到的情况,这是在 Swift 项目中使用 Objective C 代码的标准方式,反之亦然。因此,我继续创建 ANDLineChartView 的实例并将其 dataSource 属性设置为我实现 ANDLineChartViewDataSource 的类的实例。

*** -[ANDLineChartView numberOfElements] 断言失败,/path/ANDLineChartView.m:158 未设置数据源。

由 ANDLineChartView.m 中的这些代码行生成:

- (NSUInteger)numberOfElements{
  if(_dataSource && [_dataSource respondsToSelector:@selector(numberOfElementsInChartView:)]){
    return [_dataSource numberOfElementsInChartView:self];
  }else{
    NSAssert(_dataSource, @"Data source is not set.");
    NSAssert([_dataSource respondsToSelector:@selector(numberOfElementsInChartView:)], @"numberOfElementsInChartView: not implemented.");
    return 0;
  }
}

我的第一个想法是我的函数作为 Objective C 选择器不可见,所以我更改了 NSObjects responds(to aSelector : Selector!) -> Bool 方法:

  public override func responds(to aSelector: Selector!) -> Bool {
    if aSelector == #selector(numberOfElements(in:)) ||
       aSelector == #selector(numberOfGridIntervals(in:)) ||
       aSelector == #selector(chartView(_:valueForElementAtRow:)) ||
       aSelector == #selector(chartView(_:descriptionForGridIntervalValue:)) ||
       aSelector == #selector(maxValueForGridInterval(in:)) ||
       aSelector == #selector(minValueForGridInterval(in:)) {
         return true
     } else {
         return super.responds(to: aSelector)
    }
  }

这是我对图表和数据源的声明:

third = ANDLineChartView(frame : CGRect(x : 0,
                                        y : 0,
                                        width : UIScreen.main.bounds.width,
                                        height : Main.screenSegment * 9.5))

third.dataSource = GoalData(data: UserData.past)

其中 GoalData 是我的 ANDChartViewDataSource 类,它使用数组 UserData.past 进行初始化。

虽然我继续收到相同的错误消息,但在这一点上,特别是因为我以前在我的项目中甚至没有使用过桥接头等,所以我来这里寻求帮助。我也不了解 Objective C,因此与在 Objective C 中重写我的数据源类无关的解决方案会很棒。或者,如果您对用 Swift 编写的图表 API 有任何建议,那也会很有帮助,因为那样我就不必担心这个了。但我也真的很好奇,所以...

非常感谢您的帮助。

Similar Question

NSObjectProtocol Documentation

最佳答案

问题是我刚刚将 dataSource 分配给了 GoalData,但 dataSource 是一个弱属性,因此除非有其他东西使我的 GoalData 实例保持事件状态,否则它将变为 nil 并导致应用程序失败,因为 dataSource 没有值(value)。

我将其添加到实现我的 ANDLineChartView 实例的类中:

var dataSource : GoalData?

然后我在初始化程序中添加了:

self.dataSource = GoalData(data : UserData.past)
third = ANDLineChartView(frame : CGRect(x : 0,
                                    y : 0,
                                    width : UIScreen.main.bounds.width,
                                    height : Main.screenSegment * 9.5))

third.dataSource = self.dataSource

关于ios - 问题注意 Swift 方法作为 Objective C 中的选择器并在 Swift ios App 中使用 Objective C 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52341373/

相关文章:

swift - 在 AVAudioPlayer 中检测播放结束

ios - 如何在使用选项卡 View Controller 的页面中将标题设置为第一个导航 Controller 。?

iphone - IOS 从 MS Word 中读取文本

ios - 是否可以将 AnyClass 传递给泛型函数

ios - 需要正确的 Sql Stmt

ios - FireBase 存储下载错误域=FIRStorageErrorDomain 代码=-13000

objective-c - NSStackView 和内容拥抱优先级

objective-c - 如何显示来自 NSMutableDictionary 的 NSMutableArray 的动态值列表?

objective-c - 新的 Apple LLVM 4.0 "default synthesize"功能如何运作?

objective-c - 有没有办法删除日期选择器的 "year"?