我无法将 Swift 类导入 Objective-C。
- Xcode 版本:10.1
- swift :4.2
我的步骤:
创建名为“test”的空单 View Objective-C 项目。
创建 Swift 类 (mytestclass.swift)
创建桥接空头“test-Swift.h”
尝试像这样在
ViewController.m
中使用:#import "test-Swift.h" [[MyTestClass init] 计算];
我的测试类.swift
import Foundation
@objc public class MyTestClass: NSObject {
@objc public func calculate() {
print("TEST")
}
}
编译后得到:
/Users/xxx/Desktop/test/ViewController.m:19:7: Use of undeclared identifier 'MyTestClass'
我错过了什么?
最佳答案
你的问题在步骤
3- Create bridging empty header "test-Swift.h"
您不应该手动创建该文件,因为它是在您将 swift 文件添加到您的 Objective-C 时创建的,您还需要替换这一行
[[MyTestClass init] calculate];
与
[[[MyTestClass alloc] init] calculate];
或
[[MyTestClass new] calculate];
这是一个有效的 demo
关于ios - 将 Swift 导入 Objective-C - "use of undeclared identifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54181884/