[英]Swift framework - Objective-C header not found
I created a small test framework in Swift
. 我在Swift
创建了一个小型测试框架。 My purpose is to use Swift
class in Objective-C
class. 我的目的是在Objective-C
类中使用Swift
类。 I created a Swift
file. 我创建了一个Swift
文件。 And then an Objective-C
file. 然后是一个Objective-C
文件。 I made sure that the Objective-C
header in Build Settings is Test-Swift.h 我确保构建设置中的Objective-C
标头是Test-Swift.h
When I try to import Test-Swift.h in objective-C file, I get an error - 当我尝试在目标C文件中导入Test-Swift.h时,出现错误-
'Test-Swift.h' file not found. 找不到“ Test-Swift.h”文件。
Swift class : 迅捷类:
import Foundation
@objc class MyTest: NSObject {
func addTest() {
print("test")
}
}
Objective-C class : Objective-C课程:
#import "YourTest.h"
#import <Test/Test-Swift.h>
#import "Test-Swift.h" // Error in this line
@implementation YourTest
-(void)testTest {
// This is how I want to use the swift class
MyTest *test = [MyTest new];
[test addTest];
}
@end
Framework header : 框架头:
#import <UIKit/UIKit.h>
//! Project version number for Test.
FOUNDATION_EXPORT double TestVersionNumber;
//! Project version string for Test.
FOUNDATION_EXPORT const unsigned char TestVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <Test/PublicHeader.h>
Build setting (objective-C header) : 构建设置(objective-C标头):
I have set Defines Module to Yes 我已将“ 定义模块 ”设置为“ 是”
The syntax for importing Swift code within a framework target is 在框架目标中导入Swift代码的语法是
#import <ProductName/ProductModuleName-Swift.h>
which in your case would probably be 在你的情况下可能是
#import <Test/Test-Swift.h>
This line is, actually, present in your example and the compiler evidently doesn't complain about it. 实际上,这行出现在您的示例中,并且编译器显然没有抱怨它。 See https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c . 参见https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c 。
Another problem is that you won't be able to use your Swift class the way you are trying to, given the Swift declaration. 另一个问题是,鉴于Swift声明,您将无法以尝试的方式使用Swift类。 According to the above reference, the Swift declarations would need to have public
or open
access modifier in order to be visible via the -Swift.h
interface header. 根据以上参考,Swift声明将需要具有public
或open
访问修饰符,以便通过-Swift.h
接口标头可见。 You also need to make the addTest()
function callable from Objective-C, hence the need for @objc
in front of it. 您还需要使Objective-C中的addTest()
函数可调用,因此在其前面需要@objc
。 So, your Swift code should be changed like this: 因此,您的Swift代码应该像这样更改:
@objc public class MyTest: NSObject {
@objc public func addTest() {
print("test")
}
}
Based on the same documentation referenced above, you could make it more difficult for Objective-C code outside of the framework to access addTest()
by dropping public
: 基于上面引用的相同文档,您可以通过删除public
来使框架之外的Objective-C代码访问addTest()
更加困难:
@objc func addTest()...
(you still need @objc
, though). ( @objc
,您仍然需要@objc
)。
Then in your Objective-C class you could do: 然后,在Objective-C类中,您可以执行以下操作:
[test performSelector:@selector(addTest)];
However, any code outside of the framework can do the same thing. 但是,框架之外的任何代码都可以做同样的事情。
Another observation is that the line of code that caused the error in your example, ie #import "Test-Swift.h"
, is a way to include the interface header in an application target as opposed to a framework. 另一个观察结果是,在示例中导致错误的代码行(即#import "Test-Swift.h"
)是将接口标头包含在应用程序目标中而不是框架中的一种方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.