Module compiled with Swift 4.0 cannot be imported

2019-01-16 11:59发布

问题:

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

  • Base SDK iOS 11.1 for both
  • Swift Language Version Swift 4.0 for both
  • Not using Pods/Carthage

I hope someone might know

回答1:

Update:

For release versions of 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.

To fix the issue, update and rebuild your frameworks using Carthage ( carthage update --platform iOS), Cocoapods (pod update or 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.

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

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)

For beta versions of 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.

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.



回答2:

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:

carthage update --platform iOS

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



回答3:

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

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.

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

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



回答4:

I hit this issue with Xcode 10 beta, which adds Swift 4.2

Go to Build Settings > Swift Language Version for all of your Targets.

Mine were all set to 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

I then set each Target Swift Language Version to Swift 4.2, which eliminated the error.

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



回答5:

In my case (Xcode 9.2):

I went to Xcode Preferences > Locations > Derived Data

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