ios 无法在 Swift 4.0.1 中导入使用 Swift 4.0 编译的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46726077/
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-08-31 09:34:54  来源:igfitidea点击:

Module compiled with Swift 4.0 cannot be imported in Swift 4.0.1

iosswiftxcodexcode9swift4

提问by Simon

However I have recompiled the framework using the same Xcode and it still gives me this error.

但是,我使用相同的 Xcode 重新编译了框架,但它仍然给我这个错误。

  • Base SDK iOS 11.1 for both
  • Swift Language Version Swift 4.0 for both
  • Not using Pods/Carthage
  • 两者的基本 SDK iOS 11.1
  • Swift 语言版本 Swift 4.0 适用于两者
  • 不使用 Pods/Carthage

I hope someone might know

我希望有人知道

回答by Zig

Update:

更新:

For release versions of Xcode:

对于 Xcode 的发布版本:

This error (and similar errors involving Swift 4.1, 4.2, etc.) will occur when opening a project with Xcode 9.1, 9.2, 9.3, 9.4, 10, etc. that uses frameworks that were built with earlier Xcode tools.

当使用使用早期 Xcode 工具构建的框架的 Xcode 9.1、9.2、9.3、9.4、10 等打开项目时,会发生此错误(以及涉及 Swift 4.1、4.2 等的类似错误)。

To fix the issue, update and rebuild your frameworks using Carthage ( carthage update --platform iOS), Cocoapods (pod updateor pod install), or manually, with the new updated Xcode tools. The tools should be updated automatically when you update Xcode, but if not, you can follow the steps outlined below in the original answer.

要解决此问题,请使用 Carthage ( carthage update --platform iOS)、Cocoapods (pod updatepod install) 或手动使用新更新的 Xcode 工具更新和重建您的框架。当您更新 Xcode 时,这些工具应该会自动更新,但如果没有,您可以按照以下原始答案中概述的步骤进行操作。

You may also need to clean your project cmd + shift + kand possibly your build folder cmd + option + shift + kto get Xcode to not use cached framework builds.

您可能还需要清理您的项目cmd + shift + k和构建文件夹,cmd + option + shift + k以使 Xcode 不使用缓存的框架构建。

In some cases you may also need to delete your derived data folder (Easily found by going to Xcode Preferences -> Locations -> Derived Data Folder (Thanks Stunner)

在某些情况下,您可能还需要删除派生数据文件夹(通过转到 Xcode Preferences -> Locations -> Derived Data Folder 很容易找到(感谢 Stunner

For beta versions of Xcode:

对于 Xcode 的测试版:

See original answer below and then follow steps above.

请参阅下面的原始答案,然后按照上面的步骤操作。

Original Answer:

原答案:

You probably still have your xcodebuild tools set to Xcode 9.0 which builds with Swift 4.0 and is incompatible with Xcode 9.1 beta's Swift 4.0.1.

您可能仍然将 xcodebuild 工具设置为 Xcode 9.0,它使用 Swift 4.0 构建并且与 Xcode 9.1 beta 的 Swift 4.0.1 不兼容。

Check in the terminal using the command:

使用以下命令在终端中签入:

xcodebuild -version

Or just go into Xcode preferences -> Locations and check/change the command line tools to Xcode 9.1. You should be set then.

或者只需进入 Xcode 首选项 -> 位置并检查/将命令行工具更改为 Xcode 9.1。那你应该被设置了。

回答by Stunner

My issue was due to an external framework not being compiled for Swift 4.0.2. I had added this file via Carthage, so running:

我的问题是由于没有为 Swift 4.0.2 编译外部框架。我通过Carthage添加了这个文件,所以运行:

carthage update --platform iOS

And then cleaning the project cmd + shift + kand build folder cmd + option + shift + kand deleting the derived data folder (merely moving it to trash works as well) worked for me.

然后清理项目cmd + shift + k和构建文件夹cmd + option + shift + k并删除派生数据文件夹(仅将其移至垃圾箱也有效)对我有用。

回答by Emre Gürses

In my case, I downloaded xCode 11 Beta version to test SwiftUI. When i run my project, i got "Module compiled with Swift 5.1 cannot be imported by the Swift 5.0.1 compiler:..."error.

就我而言,我下载了 xCode 11 Beta 版来测试 SwiftUI。当我运行我的项目时,出现“Swift 5.1 编译器无法导入使用 Swift 5.1 编译的模块:...”错误。

To do fix this issue. i changed xCode command line tool to new xCode 11 comment line tool. After that, you can create new xcode project with SwiftUI.

要解决这个问题。我将 xCode 命令行工具更改为新的 xCode 11 注释行工具。之后,您可以使用 SwiftUI 创建新的 xcode 项目。

Xcode -> Preferences -> Location -> XCode command Line Tool

Xcode -> 首选项 -> 位置 -> XCode 命令行工具

Xcode Commend Line Tool

Xcode 命令行工具

i hope, this will help someone who want to test with new freamework :)

我希望,这会帮助那些想用新的框架进行测试的人:)

回答by Tina

Install Xcode Toolchain of specific Xcode version which was working for you from here.

这里安装适用于您的特定 Xcode 版本的 Xcode 工具链。

An Xcode toolchain includes a copy of the compiler, lldb, and other related tools needed to provide a cohesive development experience for working in a specific version of Swift.

Xcode 工具链包括编译器、lldb 和其他相关工具的副本,这些工具可为在特定版本的 Swift 中工作提供有凝聚力的开发体验。

Open Xcode's Preferences, navigate to Components > Toolchains , and select the installed Swift toolchain.

打开 Xcode 的 Preferences,导航到 Components > Toolchains ,然后选择已安装的 Swift 工具链。

This will let you compile and run the app for now.

这将让您现在编译和运行应用程序。

回答by pkamb

I hit this issue with Xcode 10 beta, which adds Swift 4.2. My issue was unrelated to Carthage or any external dependencies.

我在 Xcode 10 beta 中遇到了这个问题,它增加了 Swift 4.2。我的问题与 Carthage 或任何外部依赖项无关。

Go to YourTarget > Build Settings > Swift Language Versionfor all of your Xcode targets.

转到YourTarget > Build Settings > Swift Language Version所有 Xcode 目标。

Mine were all set to Swift 4.

我的都设置为Swift 4.

After adding a Copy Files Build Phase, I started getting the error

添加复制文件构建阶段后,我开始收到错误

:0: error: module compiled with Swift 4.1.2 cannot be imported by the Swift 4.2 compiler: /Users/user/Library/Developer/Xcode/DerivedData/MyApp-faskfrihumfcdibjxisjvbfshppp/Build/Intermediates.noindex/MyApp.build/Debug/MyAppExtension.build/Objects-normal/x86_64/MyClass~partial.swiftmodule

Command MergeSwiftModule failed with a nonzero exit code

:0: 错误:Swift 4.2 编译器无法导入使用 Swift 4.1.2 编译的模块:/Users/user/Library/Developer/Xcode/DerivedData/MyApp-faskfrihumfcdibjxisjvbfshppp/Build/Intermediates.noindex/MyApp.build/Debug/ MyAppExtension.build/Objects-normal/x86_64/MyClass~partial.swiftmodule

命令 MergeSwiftModule 失败,退出代码非零

I then set each target's Swift Language Versionto Swift 4.2, which eliminated the error.

然后我将每个目标设置Swift Language VersionSwift 4.2,从而消除了错误。

I'm not sure why the error was occuring in the first place, though, as having all targets set to Swift 4should be equally as valid.

不过,我不确定为什么首先会发生错误,因为将所有目标设置为Swift 4同样有效。

回答by user3288482

In my case (Xcode 9.2):

就我而言(Xcode 9.2):

I went to Xcode Preferences > Locations > Derived Data

我去了Xcode Preferences > Locations > Derived Data

I then deleted the data and did a clean build and was ok.

然后我删除了数据并进行了干净的构建并且没问题。

回答by 0xa6a

In my case, I used CocoaPods with plugin cocoapods-binaryto prebuild libraries to binaries. After I upgrade Xcode from 10 to 11, Xcode gives me this error:

就我而言,我使用带有插件cocoapods-binary的 CocoaPods 将库预构建为二进制文件。在我将 Xcode 从 10 升级到 11 后,Xcode 给了我这个错误:

Module compiled with Swift 5.0.1 cannot be imported by the Swift 5.1 compiler

I fix it by removing the /Podsdirectory and running pod install.

我通过删除/Pods目录并运行pod install.