将Swift导入到Objective-C中-“使用未声明的标识符”

沃西切·库里克

我无法将Swift类导入Objective-C。

  • Xcode版本:10.1
  • 斯威夫特:4.2

我的步骤:

  1. 创建一个空的Single-View Objective-C项目,称为“测试”。

  2. 创建Swift类(mytestclass.swift)

  3. 创建桥接空标题“ test-Swift.h”

  4. 尝试ViewController.m像这样使用#import "test-Swift.h" [[MyTestClass init] calculate];

mytestclass.swift

import Foundation

@objc public class MyTestClass: NSObject {

    @objc public func calculate() {
        print("TEST")
    }
}

编译后,我得到:

/Users/xxx/Desktop/test/ViewController.m:19:7:使用未声明的标识符'MyTestClass'

我想念什么?

Sh_Khan

您的问题已解决

3-创建桥接空标题“ test-Swift.h”

您不应该手动创建该文件,就像将swift文件添加到Objective-C时创建的那样,您还需要替换此行

[[MyTestClass init] calculate];

[[[MyTestClass alloc] init] calculate];

要么

[[MyTestClass new] calculate];

这是一个工作演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章