Xcode 6 / Beta 4:不支持使用带有框架目标的桥接头文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24875745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:08:03  来源:igfitidea点击:

Xcode 6 / Beta 4: using bridging headers with framework targets?is unsupported

xcodeframeworksbeta

提问by Chris Conover

I just upgraded to Xcode 6 Beta 4 and have a framework that I created for Live Views in Beta 2. Due to another swift bug, I needed to use some Obj-C code. When upgrading though, I get the following error:

我刚刚升级到 Xcode 6 Beta 4 并且有一个我为 Beta 2 中的实时视图创建的框架。由于另一个 swift错误,我需要使用一些 Obj-C 代码。但是升级时,我收到以下错误:

error: using bridging headers with framework targets?is unsupported

错误:不支持将桥接头与框架目标一起使用?

I have not seen anything in the release notes, or found any other migration path. Has anyone else seen this and arrived at a solution?

我在发行说明中没有看到任何内容,也没有找到任何其他迁移路径。有没有其他人看到过这个并找到了解决方案?

I realize that Beta 3 eliminated the need for frameworks for live views, but it makes sense in my case if I can get it to work. I can remove it though as a fallback, but would prefer to use a framework if they are not totally broken in Beta 4.

我意识到 Beta 3 消除了对实时视图框架的需求,但在我的情况下,如果我可以让它工作,这是有道理的。我可以将其删除作为后备,但如果它们在 Beta 4 中没有完全损坏,我更愿意使用框架。

回答by DeepFriedTwinkie

As the error states, bridging headers are not allowed in Frameworks. The Importing Code from Within the Same Framework Targetsection of the Mix & Matchapple documentation hints at this. As they say, you need to "In your umbrella header file, import every Objective-C header you want to expose to Swift".

正如错误所述,框架中不允许使用桥接头。在从在同一框架内目标导入代码中的部分混合&搭配在这个苹果文档的提示。正如他们所说,你需要“在你的伞头文件中,导入你想要暴露给 Swift 的每个 Objective-C 头文件”。

However, I discovered that you may also need to make those specific headers public as well. This answer reviews why and how to do that: Swift compiler error: "non-modular header inside framework module".

但是,我发现您可能还需要公开这些特定的标头。这个答案回顾了为什么以及如何做到这一点:Swift compiler error: "non-modular header inside framework module"

So, do this:

所以,这样做:

  1. Remove your bridging header file.
  2. Remove references to the bridging header file in the build settings for the framework
  3. Add the necessary headers to your umbrella file ([ProductName].h)
  4. Make the included files public in the framework's "Headers" section of its "Build Phases".
  5. Clean and rebuild.
  1. 删除您的桥接头文件。
  2. 在框架的构建设置中删除对桥接头文件的引用
  3. 将必要的标题添加到您的伞文件 ([ProductName].h)
  4. 在框架的“构建阶段”的“标题”部分中公开包含的文件。
  5. 清洁和重建。

Note: The "umbrella header file" is a file (named [ProductName].h) that generally represents all public headers of a framework. It is usually just a list of #import statements to other headers contained in the framework. In Xcode, if you open UIKit.h, you will see a good example of an umbrella file.

注意:“伞头文件”是一个文件(名为 [ProductName].h),通常代表框架的所有公共头文件。它通常只是框架中包含的其他标头的#import 语句列表。在 Xcode 中,如果您打开 UIKit.h,您将看到一个很好的伞形文件示例。

回答by Jan Rüegg

There are two possibilities. Adding the necessary headers to the umbrella header file and making them public is one way. However, this is a problem if the headers should be available to Swift, but not public.

有两种可能。将必要的头文件添加到伞头文件并将它们公开是一种方法。然而,如果头文件应该对 Swift 可用,但不是公开的,这是一个问题。

The second possibility which will make internal headers available to Swift is described in detail here. Essentially, a module map similar to the following needs to be created:

使内部头文件可用于 Swift 的第二种可能性在此处详细描述。本质上,需要创建类似于以下内容的模块映射:

module AwesomeKitPrivate {  
  header "../InternalClass.h"
  export *
}

This can then be included in XCode using the setting:

然后可以使用以下设置将其包含在 XCode 中:

SWIFT_INCLUDE_PATHS = $(SRCROOT)/AwesomeKit/ProjectModule  

回答by Ralph Shane

See Importing Objective-C into Swift.

请参阅将 Objective-C 导入 Swift

To import Objective-C code into Swift from the same framework

将 Objective-C 代码从同一框架导入 Swift

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes".
  2. In your umbrella header file, import every Objective-C header you want to expose to Swift. For example:

        #import "XYZ/XYZCustomCell.h"
        #import "XYZ/XYZCustomView.h"
        #import "XYZ/XYZCustomViewController.h"
    
  3. Make the included files public in the framework's "Headers" section of its "Build Phases".

  4. Clean and rebuild.

  1. 在构建设置下的打包中,确保该框架目标的定义模块设置设置为“是”。
  2. 在您的伞头文件中,导入您想要公开给 Swift 的每个 Objective-C 头文件。例如:

        #import "XYZ/XYZCustomCell.h"
        #import "XYZ/XYZCustomView.h"
        #import "XYZ/XYZCustomViewController.h"
    
  3. 在框架的“构建阶段”的“标题”部分中公开包含的文件。

  4. 清洁和重建。

Swift will see every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework will be available in any Swift file within that framework target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

Swift 会在你的伞头文件中看到你公开的每一个头文件。该框架中的 Objective-C 文件的内容将自动在该框架目标内的任何 Swift 文件中可用,无需任何导入语句。使用与系统类相同的 Swift 语法使用自定义的 Objective-C 代码。

let myOtherCell = XYZCustomCell()
myOtherCell.subtitle = "Another custom cell"

Important: the "umbrella header file" means the file {ModuleName}.h. BTW, the target name is {ModuleName}.framework.

重要提示:“伞头文件”是指文件 {ModuleName}.h。顺便说一句,目标名称是 {ModuleName}.framework。