简体   繁体   English

Swift框架-找不到Objective-C标头

[英]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声明将需要具有publicopen访问修饰符,以便通过-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.

相关问题 使用Swift和Objective-C桥接标题和框架时发生命名空间冲突 - Namespace conflict on bridging header and framework with swift and objective-c 存档应用程序时找不到Swift Objective-C标头 - Swift Objective-C header is not found when archiving the app Objective-c 使用 cocoapods 添加 Swift 框架未找到文件 - objective-c use cocoapods add Swift framework not found file 将Objective-C框架转换为Swift-头文件仍显示Objective-C函数 - Converting an Objective-C framework to Swift - Header files still displaying Objective-C functions 标有@objc的Swift类未添加到Objective-C / Swift混合框架的“ -Swift.h”标头中 - Swift classes marked with @objc not being added to “-Swift.h” header in mixed Objective-C/Swift framework 从Swift生成的Objective-C标头 - Generated Objective-C header from Swift 添加一个Swift框架的Objective-C项目,可以导入桥头,但是显示未声明的swift类 - Objective-C project adding a Swift framework, can import bridge header, but it show undeclared swift class Xcode 8与混合Swift和Objective-c项目生成的“ModuleName-Swift.h”标头未找到 - Xcode 8 with mixed Swift and Objective-c project generated “ModuleName-Swift.h” header not found 找不到Objective-C Bridging Header部分 - Objective-C Bridging Header section not found 导出Swift框架以用于Objective-C - Export Swift framework for the usage in Objective-C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM