简体   繁体   English

Swift 5.1编译的模块不能被Swift 5.1.2编译器导入

[英]Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler

I have a framework (in this instance it's RxSwift) which I've compiled using Xcode 11.0 into the traditional RxSwift.framework style package我有一个框架(在本例中是 RxSwift),我使用 Xcode 11.0 将其编译为传统的RxSwift.framework样式 package

This imported fine into Xcode 11.0 and also 11.1 never had any problems with it这很好地导入了 Xcode 11.0 和 11.1 从来没有任何问题

Today, upon Apple's release of Xcode 11.2, I upgraded, and I am presented with the error:今天,Apple 发布 Xcode 11.2,我升级后,出现错误:

Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler Swift 5.1编译的模块不能被Swift 5.1.2编译器导入

I'm used to swift compiler mismatches, and I'm aware I can just recompile RxSwift using Xcode 11.2 and carry on, but the headline feature of Swift 5.1 was module stability .我习惯了 swift 编译器不匹配,我知道我可以使用 Xcode 11.2 重新编译 RxSwift 并继续,但 Swift 5.1 的标题特性是模块稳定性

I was under the impression that now that we have module stability, frameworks won't need to keep getting recompiled with every new Xcode release, yet this is clearly not the case.我的印象是,既然我们有了模块稳定性,框架就不需要在每个新的 Xcode 版本中不断重新编译,但事实显然不是这样。

If anyone can explain what is going on here I would much appreciate it.如果有人能解释这里发生了什么,我将不胜感激。 Is Xcode 11.2 exhibiting a bug? Xcode 11.2 是否存在错误? or did I somehow need to tell it I wanted module stability when I originally compiled with Xcode 11.0?或者当我最初使用 Xcode 11.0 编译时,我是否需要以某种方式告诉它我想要模块稳定性?

OK, Turns out if you watch the WWDC video, they explain it: https://developer.apple.com/videos/play/wwdc2019/416/好的,如果你看 WWDC 视频,他们会解释: https://developer.apple.com/videos/play/wwdc2019/416/

You need to set the Build Settings > Build Options > Build Libraries for Distribution option to Yes in your framework's build settings, otherwise the swift compiler doesn't generate the neccessary .swiftinterface files which are the key to future compilers being able to load your old library.您需要在框架的构建设置中将Build Settings > Build Options > Build Libraries for Distribution选项设置为Yes ,否则 swift 编译器不会生成必要的.swiftinterface文件,这些文件是未来编译器能够加载旧版本的关键图书馆。

This ends up in your project.pbxproj file as:这最终在您的 project.pbxproj 文件中为:

BUILD_LIBRARY_FOR_DISTRIBUTION = YES;

After setting this flag, a framework I compiled using Xcode 11.0 (swift 5.1) was able to be used by Xcode 11.2 (swift 5.1.2) and everything appears to be working correctly.设置此标志后,我使用 Xcode 11.0 (swift 5.1) 编译的框架能够被 Xcode 11.2 (swift 5.1.2) 使用,并且一切似乎都正常工作。

Hopefully this question/answer will serve as a useful reference for everyone who hasn't watched all the WWDC videos希望这个问题/答案能为所有没有看过所有 WWDC 视频的人提供有用的参考

If the error still persists go to Product > Clean Build Folder and Build again.如果错误仍然存在 go 到Product > Clean Build Folder并再次构建。

Module stability and Library evolution support for closed source闭源的模块稳定性和库进化支持

[ABI] [ABI]

Swift v5.0 introduced stable ABI Swift v5.0引入稳定ABI

Swift v5.1 shipped Module stability and Library evolution support which are applicable for closed source(binary) framework(library) (framework is build separately from consumer) Swift v5.1发布Module stabilityLibrary evolution support ,适用于闭源(二进制)框架(库) (框架与消费者分开构建)

Check Swift version:查看 Swift 版本:

Swift Language Version(SWIFT_VERSION)

To enable it you should use Xcode from v11:要启用它,您应该使用 v11 中的 Xcode:

Build Libraries for Distribution (BUILD_LIBRARY_FOR_DISTRIBUTION)为分发构建库 (BUILD_LIBRARY_FOR_DISTRIBUTION)

Select framework target -> Build Settings -> Build Libraries for Distribution (BUILD_LIBRARY_FOR_DISTRIBUTION) -> Yes

swiftc flags: swiftc标志:

-enable-library-evolution
-emit-module-interface

This setting generates .swiftinterface此设置生成.swiftinterface

Swift Module Interfaces (.swiftinterface) Swift 模块接口 (.swiftinterface)

Swift Module uses the same approach as Objective-C module uses - precompiled binary or Compiled Module . Swift Module使用与 Objective-C 模块相同的方法 - precompiled binaryCompiled Module

Swift Module Interfaces is a textual representation of module's public API. Swift Module Interfaces是模块公共 API 的文本表示。 It is an Swift's alternative for Objective-C's headers .h files.它是 Swift 的 Objective-C 头文件.h文件的替代品。

//previously
consumer(app) -> import Module -> producer(framework) .swiftmodule

//using .swiftinterface
consumer(app) -> import Module -> .swiftinterface -> producer(framework) .swiftmodule

Despite of .swiftmodule which is changeable where you can get尽管.swiftmodule是可变的,你可以在哪里获得

Module compiled with _ cannot be imported by the _ compiler

.swiftinterface is stable and do not need to be update when something changed(eg Swift version) .swiftinterface是稳定的,当有变化时不需要更新(例如 Swift 版本)

no assamptions没有选择

It is located in next folder它位于下一个文件夹中

<framework_name>.framework/Modules/<framework_name>.swiftmodule

It looks like:看起来像:

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
// swift-module-flags: -target x86_64-apple-ios12.2-simulator -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -Onone -module-name UtilsSwiftFramework
import Foundation
import Swift
@_exported import UtilsSwiftFramework
@_inheritsConvenienceInitializers @objc public class GFISClassA : ObjectiveC.NSObject {
  @objc public static var shared: UtilsSwiftFramework.GFISClassA
  @objc public func GFISprintHelloWorld()
  @objc public func GFISprintHelloWorld(arg1: Swift.String, arg2: Swift.String)
  @objc deinit
  @objc override dynamic public init()
}

As you see it additionally it contains:如您所见,它还包含:

swift-interface-format-version
swift-compiler-version
swift-module-flags

*You can get next error if you use dynamic without @objc [About] *如果你在没有@objc情况下使用dynamic ,你会得到下一个错误[关于]

Marking non-'@objc' Swift declaration 'dynamic' in library evolution mode is not supported

XCFramework [About] forces you to use it XCFramework [About]强制你使用它

Apple recommends to use .swiftinterface for closed source and Swift Package Manager [About] for open source Apple 建议将.swiftinterface用于封闭源代码, Swift Package Manager [About]用于开源

I had the same error importing 3rd party libraries.我在导入 3rd 方库时遇到了同样的错误。 I fixed it using toolchains in Xcode and taking the release September 19 2019 from here https://swift.org/download/#releases .我使用 Xcode 中的工具链修复了它,并从此处https://swift.org/download/#releases于 2019 年 9 月 19 日发布。 After that I had to re-import my libraries and it worked.之后,我不得不重新导入我的库并且它起作用了。

I started seeing the same issue and found out that I was using the incorrect xcode Command Line Tools.我开始看到同样的问题,发现我使用了不正确的 xcode 命令行工具。 Went to Preferences > Locations and changed the command line tools from Xcode 11.1 to 11.2.1.转到首选项 > 位置并将命令行工具从 Xcode 11.1 更改为 11.2.1。 Just to be safe, I also cleared the derived data directory and ran carthage build .为了安全起见,我还清除了派生数据目录并运行carthage build

Distribution (BUILD_LIBRARY_FOR_DISTRIBUTION) -> Yes & Pod update - the error went away for me. Distribution (BUILD_LIBRARY_FOR_DISTRIBUTION) -> Yes & Pod update - 这个错误对我来说消失了。

This made my compiler error go away.这使我的编译器错误 go 消失了。

  1. carthage bootstrap --platform ios
  2. brew bundle
  3. pod repo update

I experienced this on repo after moving back to Xcode 11.3.1 from Xcode 12.3.从 Xcode 12.3 移回 Xcode 11.3.1 后,我在回购中遇到了这种情况。 I had summarily updated my Command Line Tools and needed to revert them to the earlier version in order to get my dependency building.我已经快速更新了我的命令行工具,并且需要将它们恢复到早期版本才能建立我的依赖关系。

Fixed the error by running command from project's root folder:通过从项目的根文件夹运行命令修复了错误:

carthage update --use-xcframeworks --platform iOS --cache-builds

If even after trying all the steps mentioned above, it is not resolved then try to see what all prebuild folders are present in your repo which is not directly related to your project.如果即使在尝试了上述所有步骤之后,它仍然没有解决,那么请尝试查看您的存储库中存在哪些与您的项目没有直接关系的所有预构建文件夹。 Delete all of them, delete pods folder and podfile.lock and clean derived data and then try to build.删除所有这些,删除 pods 文件夹和 podfile.lock 并清理派生数据,然后尝试构建。

It worked for me:D它对我有用:D

One other way I managed to fix this was just double checking the module you're using and seeing if there's an update for it.我设法解决此问题的另一种方法是仔细检查您正在使用的模块并查看它是否有更新。 I was using ViewControllerPresentationSpy to test alert controllers in my project.我在我的项目中使用 ViewControllerPresentationSpy 来测试警报控制器。 I did a pod reintegrate, then pod install, to update the version from 5.1 to 6.0 as the author of that module had since released an update for it.我做了一个 pod reintegrate,然后 pod install,将版本从 5.1 更新到 6.0,因为该模块的作者已经发布了更新。 I removed the previous module's folder in my project.我在我的项目中删除了上一个模块的文件夹。 Then for the new version, I was able to drag and drop the project just above all my tests after cleaning the build and going the project navigator and adding it to the Copy Files section for the VC Presentation Spy to the project.然后对于新版本,我能够在清理构建并进入项目导航器并将其添加到项目的 VC Presentation Spy 的复制文件部分后,将项目拖放到所有测试的上方。

Long story short, double check where you got your module from and check for an update and add it again to your Xcode project.长话短说,仔细检查您从哪里获得模块并检查更新并将其再次添加到您的 Xcode 项目中。

Sometimes can help for run app on Apple M1有时可以帮助在 Apple M1 上运行应用程序

通过添加它进行调试来更改构建设置

try also in terminal:也可以在终端尝试:

arch -x86_64 pod install

I had the similar issue once I moved from xcode 13.4 to xcode 14.1 Solution was to set appropriate compiler in Xcode - Preferences - Command Line Tools我从 xcode 13.4 移动到 xcode 14.1 后遇到了类似的问题解决方案是在 Xcode - 首选项 - 命令行工具中设置适当的编译器

I ran into the same issue where I only switched branches.我遇到了同样的问题,我只切换了分支。 I deleted derived data + clean build a few times.我删除了派生数据+清理构建几次。 It didn't work until I restarted Xcode在我重新启动 Xcode 之前它没有工作

You can use Carthage to add the RxSwift framework.您可以使用 Carthage 添加 RxSwift 框架。

Basically, Carthage gives you a similar traditional RxSwift.framework style package .基本上,Carthage 为您提供了类似的传统 RxSwift.framework 样式 package

Then try carthage update --no-use-binaries .然后尝试carthage update --no-use-binaries

暂无
暂无

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

相关问题 Xcode 11.2.1 错误 - 使用 Swift 5.1 编译的模块无法由 Swift 5.1.2 编译器导入 - Xcode 11.2.1 Error - Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler Swift 5.1.2 编译器 (couchbaselite) xcode 11.2 无法导入使用 Swift 5.1 编译的模块 - Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler (couchbaselite) xcode 11.2 Swift 5.0.1 编译的模块不能被 Swift 5.1 编译器导入 - Module compiled with Swift 5.0.1 cannot be imported by the Swift 5.1 compiler (Swift Playground 模板)Swift 5.1.3 编译器无法导入使用 Swift 5.1 编译的模块 - (Swift Playground Template) Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.3 compiler 安装 Xcode10.2.1 但仍然收到错误“Swift 5.1 编译器无法导入使用 Swift 5.0.1 编译的模块” - Install Xcode10.2.1 but still got the error 'Module compiled with Swift 5.0.1 cannot be imported by the Swift 5.1 compiler' Swift 5.0.1编译的模块不能被Swift 5.1导入 - Module compiled with Swift 5.0.1 cannot be imported by the Swift 5.1 Swift 5.0 编译器无法导入使用 Swift 4.2.1 编译的模块 - Module compiled with Swift 4.2.1 cannot be imported by the Swift 5.0 compiler Swift 4.2.1编译器无法导入使用Swift 4.0.3编译的模块 - Module compiled with Swift 4.0.3 cannot be imported by the Swift 4.2.1 compiler 使用 Swift 5.3.1 编译的模块不能被 Swift 5.1.3 编译器导入 - Module compiled with Swift 5.3.1 cannot be imported by the Swift 5.1.3 compiler 使用 Swift 5.2.4 编译的模块不能被 Swift 5.3.1 编译器导入 - Module compiled with Swift 5.2.4 cannot be imported by the Swift 5.3.1 compiler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM