简体   繁体   English

在 objective-c 模块中使用 swift 时无法构建 Objective-C 模块

[英]Could not build Objective-C module, when using swift in objective-c module

In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file).在 iOS 应用程序中,我有一个子项目(不是 cocoapods),其中包含一个 swift 文件和 ObjC 文件(由 swift 文件使用)。 XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. XCode 自动创建了一个桥接文件但无法构建它,因为在框架中显然不允许桥接。 The workaround that I used was to add the objective-c header to the umbrella file and it worked.我使用的解决方法是将 objective-c 标头添加到 umbrella 文件并且它起作用了。 Now I need to use a swift class from ObjC.现在我需要使用 ObjC 的 swift 类。 I have define module to set to YES, the generated file Framework-Swift.h.我已经define module设置为YES,生成文件Framework-Swift.h。 But when I try to import it in objective-c i get但是当我尝试在 objective-c 中导入它时,我得到

Could not build Objective-C module无法构建 Objective-C 模块

The closest I got after some googleing was this answer:经过一些谷歌搜索后,我得到的最接近的答案是:

Ah gotcha.啊明白了。 It looks like you're building a mixed Swift & Objective-C pod - if that's the case, Xcode will try to import within the generated -Swift.h header.看起来您正在构建混合的 Swift 和 Objective-C pod - 如果是这种情况,Xcode 将尝试在生成的 -Swift.h 标头中导入。 You'll need to create the header manually and add imports for the Objective-C classes that you want to expose to Swift.您需要手动创建标头并为要公开给 Swift 的 Objective-C 类添加导入。

CocoaPods generates an umbrella header automatically and imports it within the.modulemap, but Xcode doesn't use that when generating the -Swift.h header CocoaPods 自动生成一个伞形标头并将其导入到 .modulemap 中,但 Xcode 在生成 -Swift.h 标头时不使用它

But I am unsure what header needs to be created manually.但我不确定需要手动创建什么标头。 Any ideeas or pointer about using swift in an objective-c framework?关于在 objective-c 框架中使用 swift 的任何想法或指示? In both ways?两种方式?

I also had similar issue when using Swift pods in my Swift project containing several targets.在包含多个目标的 Swift 项目中使用 Swift pod 时,我也遇到了类似的问题。 No Objective-C code at all.根本没有 Objective-C 代码。 I tried to clear build folder, pods cache, derived data - nothing worked.我试图清除构建文件夹、pods 缓存、派生数据——没有任何效果。

Solution:解决方案:

Open the Build Settings for a target that contains your module code.打开包含模块代码的目标的构建设置。 Set the "Install Objective-C Compatibility Header" to "No"将“安装 Objective-C 兼容性标头”设置为“否”

在此处输入图像描述

There's a great and simple article that wraps up this case:一篇很棒而简单的文章总结了这个案例:

  1. DEFINES_MODULE=YES DEFINES_MODULE=YES
  2. To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings要在 Swift 中使用 ObjC 类,请创建一个桥接标头并在构建设置中指定它的路径
  3. To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use @class SwiftClass; forward declaration in *.h file, if needed)要在 ObjC 中使用 Swift 类,请在*.m文件中使用#import <ModuleName/ModuleName-Swift.h> (使用@class SwiftClass;如果需要,在*.h文件中进行前向声明)
  4. For swift classes and their members to be visible to objc让 swift 类及其成员对 objc 可见
    1. inherit your class from NSObjectNSObject继承你的类
    2. prepend it with @objcMembers在它前面加上@objcMembers
    3. make both the class and its members public public班级及其成员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM