我无法将Swift类导入Objective-C。
- Xcode版本:10.1
- Swift:4.2
我的步骤:
-
创建一个名为"测试"的空的单视图Objective-C项目。
-
创建Swift类(mytestclass.Swift)
-
创建桥接空头"testSwift.h">
-
尝试在
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">
我缺少什么?
您的问题在步骤中
3-创建桥接空标头"testSwift.h">
您不应该手动创建该文件,因为它是在您将swift文件添加到Objective-C时创建的,您还需要替换此行
[[MyTestClass init] calculate];
带有
[[[MyTestClass alloc] init] calculate];
或
[[MyTestClass new] calculate];
这是的工作演示