【问题标题】:Call custom Objective-C methods in Swift?在 Swift 中调用自定义的 Objective-C 方法?
【发布时间】:2014-12-06 02:21:35
【问题描述】:

我目前正在网上学习几个 Swift 课程来弄清楚这个新语法,但我觉得我遇到了一堵我无法真正弄清楚的墙。

我的问题是:我需要做什么才能在 Swift 中调用自定义的 Objective-C 方法?为什么 Swift 无法识别代码的来源?为什么会出现“UIViewController 没有名为‘customMethod’的成员”之类的错误?是导入问题吗?

既然你知道我的问题,这就是我问的原因和背景

我有一个调用此方法的objective-c .m 文件:

- (void)configureMyApp
{
// overridden by subclasses
}    

然后这个:

- (void)addAppElement:(MyAppElement *)element
{
  [self addAppElement atIndex:[self.elements count]];
}

在我的 Swift 应用程序中,我设置了桥接头并将 .h 文件导入到所述头文件中

我尝试通过这个 Swift 函数调用 configureMyApp 方法:

func configureMyApp()
{

self.addAppElement...
}

但它无法识别该函数来自我导入的 Objective-c 文件,myswiftfile.swift 也无法识别 addAppElement

在 obj-c 中我会这样称呼它:

- (void)configureMyApp
{
[super configureMyApp];

[self addAppElement:.....];
}

我还需要做些什么来导入 obj-c 代码吗?我可能会错过什么?

【问题讨论】:

标签: objective-c function methods swift


【解决方案1】:

此错误:UIViewController does not have a member named 'customMethod' 表示您尝试在类型为 UIViewController 的对象上调用 customMethod,而不是作为 UIViewController 的自定义子类。

【讨论】:

  • 知道了!我忘了你需要改变子类
猜你喜欢
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多