简体   繁体   English

标有@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

I am attempting to convert an old statically linked library to a framework. 我正在尝试将旧的静态链接库转换为框架。 With mixed swift and objective c in the static library, all headers are generated correctly. 在静态库中混合使用swift和目标c时,可以正确生成所有标头。 However, switching to a framework target and adding swift files, marked with the @objc header, the class is not added to the -Swift.h header. 但是,切换到框架目标并添加带有@objc标头的swift文件时,该类不会添加到-Swift.h标头中。 I can import the header, but swift classes are not found. 我可以导入标头,但找不到快速类。 This is in Xcode 10.2 and attempted using both Swift 4.2 and 5. 这在Xcode 10.2中,并且尝试同时使用Swift 4.2和5。

Are there any particular settings in XCode that will affect the generation of the *-Swift.h header in a mixed Objective C/Swift framework target? XCode中是否有任何特定设置会影响混合的Objective C / Swift框架目标中* -Swift.h标头的生成?

I had a similar issue. 我有一个类似的问题。 In my case it was a known issue in Xcode 10.2: 就我而言,这是Xcode 10.2中的一个已知问题:

https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes

If you're building a framework containing Swift code and using lipo to create a binary that supports both device and simulator platforms, you must also combine the generated Framework-Swift.h headers for each platform to create a header that supports both device and simulator platforms. 如果您要构建一个包含Swift代码的框架,并使用lipo创建同时支持设备和模拟器平台的二进制文件,则还必须为每个平台组合生成的Framework-Swift.h标头,以创建同时支持设备和模拟器的标头平台。 (48635615) ... (48635615)...

In my case all I had to do was to update the Carthage to the newest version 0.33.0 就我而言,我要做的就是将Carthage更新为最新版本0.33.0

The problem appears to be a combination of Apple's new build system, the expectations they set when compiling and the number of inter-dependencies in the project setup. 问题似乎是苹果新的构建系统,他们在编译时设定的期望以及项目设置中相互依存关系的数量的结合。

The new build system runs the Swift compilations in parallel. 新的构建系统并行运行Swift编译。 When having multiple library/framework dependencies that are mixed Objective C and Swift, the compiler appears to not generate the -Swift.h files on time. 当具有多个目标库和Swift混合的库/框架依赖项时,编译器似乎未按时生成-Swift.h文件。 In static libraries, the -Swift.h files appear to be generated at the end of the Swift Compilation process, meaning they are not generated quickly enough to be used by the Objective C files when the Objective C compilation occurs. 在静态库中,-Swift.h文件似乎是在Swift编译过程结束时生成的,这意味着在进行Objective C编译时,生成它们的速度不够快,无法由Objective C文件使用。 When generating a framework, it appears that the Compiler generates the header at the beginning of the compilation process and the Swift files are not fully compiled and the -Swift.h file does not generate appropriately with the Objective C class interfaces and protocols. 生成框架时,似乎编译器会在编译过程开始时生成标头,并且Swift文件未完全编译,并且-Swift.h文件无法使用Objective C类接口和协议正确生成。

What this means ends up meaning is that we can not rely on the "target dependencies" to build the dependent projects correctly. 这意味着最终的意思是,我们不能依靠“目标依赖项”来正确构建依赖项。

So how can we build our .framework of mixed Objective C and -Swift.h without a ton of manual scripting. 因此,如何在没有大量手动脚本的情况下构建混合了Objective C和-Swift.h的.framework。

Here are the tricks I discovered that will work. 这是我发现将起作用的技巧。

  1. Use the old build system. 使用旧的构建系统。 When using the new build system there is an error when it attempts to merge the module map for the static library file saying that it can not find the *-Swift.h file whether or not it exists. 当使用新的构建系统时,尝试合并静态库文件的模块映射时出错,表明它找不到* -Swift.h文件,无论它是否存在。
  2. Create your framework by making it a wrapper around the static library by: 通过以下方法使框架成为静态库的包装器来创建框架:
    • Giving them both the same product name and module name. 给他们两个相同的产品名称和模块名称。
    • Add a single .Swift file to the framework build so that it has something to compile and will link the swift libraries. 向框架构建中添加一个.Swift文件,以便它可以编译并链接swift库。
    • link to the static library in the framework. 链接到框架中的静态库。
    • Add all necessary headers to the public headers of the framework. 将所有必需的标头添加到框架的公共标头。
    • Add all public headers to the umbrella header. 将所有公共标头添加到伞形标头中。
    • Use a Run script phase to copy the *-Swift.h file from the static library build to the framework product post compile. 使用运行脚本阶段可将* -Swift.h文件从静态库版本复制到编译后的框架产品。
    • For any public headers that include the *-Swift.h, you may need to post process the header and replace the *-Swift.h import with the appropriate framework import ie . 对于任何包含* -Swift.h的公共标头,您可能需要对标头进行后期处理,并将* -Swift.h导入替换为适当的框架import。 This would not be recommended due to possible cyclical imports in the umbrella header. 由于伞头中可能存在周期性导入,因此不建议这样做。
  3. When running a clean, build the framework target first manually, before building the application target. 运行干净时,请先手动构建框架目标,然后再构建应用程序目标。

Below is an example script for copying the *-Swift.h file post build. 以下是用于复制* -Swift.h文件发布版本的示例脚本。

header_file="${TARGET_TEMP_DIR}/../${PRODUCT_MODULE_NAME}.build/DerivedSources/${PRODUCT_MODULE_NAME}-Swift.h"
header_dir="${BUILT_PRODUCTS_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}"
mkdir -p "$DIR"
echo "copying $header_file $header_dir"
cp -f "$FILE" "$DIR"

UPDATED 更新

Marking all modules Swift compilation mode to "Whole Module" appears to have a positive affect on this issue, but I have not fully tested it. 将所有模块都标记为Swift编译模式为“ Whole Module”似乎对这个问题有积极的影响,但是我还没有对其进行全面的测试。

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

相关问题 Objective-C和Swift混合项目-Swift.h文件报告错误 - Objective-c and Swift mixed project -Swift.h file report errors Objective-C单元测试中的Swift类 - 未完成生成的“-Swift.h” - Swift classes inside Objective-C unit test - Incomplete Generated “-Swift.h” 在objective-c中导入swift类,<myModule> -Swift.h 文件未找到 - import swift class in objective-c, <myModule>-Swift.h file not found 制作混合的Objective-C和Swift框架 - Making a mixed Objective-C & Swift Framework Swift &amp; Objective-C 项目 - 生成 -Swift.h 错误 - 找不到 UIViewController 的接口声明 - Swift & Objective-C project - generated -Swift.h error - Cannot find interface declaration for UIViewController Xcode 8与混合Swift和Objective-c项目生成的“ModuleName-Swift.h”标头未找到 - Xcode 8 with mixed Swift and Objective-c project generated “ModuleName-Swift.h” header not found Swift框架-找不到Objective-C标头 - Swift framework - Objective-C header not found Swift“Bridging-Header.h”文件不允许我在.swift文件中实例化objective-c类 - Swift “Bridging-Header.h” file not allowing me to instantiate objective-c classes in .swift files 使用混合的Swift和Objective-C代码创建框架 - Creating a framework with mixed Swift & Objective-C code 在伞头中包含-Swift.h - Include -Swift.h in the umbrella header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM