iOS Swift 项目,已经导入了 Objective-C 文件,我想导入一个 swift class 到这个 Objective-C
iOS Swift project, already imported Objective-C file and I want to import a swift class into this Objective-C
我有一个 Swift 项目,我成功地将一些 Objective C class 文件添加到这个 Swift 项目中并且能够在项目中使用它,使用桥接头。现在在ObjectiveClass.hclass中,我想导入一个Swiftclass的Swift项目。我该怎么做?
我已经搜索了很多,但还没有找到任何答案。大多数回答的问题都是关于如何将 Objective-C class 导入 Swift 项目,或将 Swift class 导入 Objective-C 项目。但我的情况不同
直接来自 appledoc。
When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".
大多数人不明白的是,xyz-Swift.h
头文件将由 xcode 生成,您只需要相信这一点。起初我正在搜索整个项目源文件夹,这个文件在哪里,找不到它。您只需要导入它,瞧。有时你会收到类似 xyz-Swift.h
not found 的错误消息,请清理你的项目,它将顺利运行。
很简单。只需在 swift class 之前声明 @objc public
。像这样
@objc public class YourSwiftClass : NSObject {
}
现在在 objective c class 使用 #import "YourProjectName-Swift.h"
。您将收到未找到上述文件的错误消息。别着急,运行项目一次就好了。当您在 运行 时单击该文件时,您将看到 "YourProjectName-Swift.h" 的内容。现在您可以访问 swift class.
的任何方法
我有一个 Swift 项目,我成功地将一些 Objective C class 文件添加到这个 Swift 项目中并且能够在项目中使用它,使用桥接头。现在在ObjectiveClass.hclass中,我想导入一个Swiftclass的Swift项目。我该怎么做?
我已经搜索了很多,但还没有找到任何答案。大多数回答的问题都是关于如何将 Objective-C class 导入 Swift 项目,或将 Swift class 导入 Objective-C 项目。但我的情况不同
直接来自 appledoc。
When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".
大多数人不明白的是,xyz-Swift.h
头文件将由 xcode 生成,您只需要相信这一点。起初我正在搜索整个项目源文件夹,这个文件在哪里,找不到它。您只需要导入它,瞧。有时你会收到类似 xyz-Swift.h
not found 的错误消息,请清理你的项目,它将顺利运行。
很简单。只需在 swift class 之前声明 @objc public
。像这样
@objc public class YourSwiftClass : NSObject {
}
现在在 objective c class 使用 #import "YourProjectName-Swift.h"
。您将收到未找到上述文件的错误消息。别着急,运行项目一次就好了。当您在 运行 时单击该文件时,您将看到 "YourProjectName-Swift.h" 的内容。现在您可以访问 swift class.