92

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

Simon
  • 2,419
  • 2
  • 18
  • 30

9 Answers9

142

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.

Stunner
  • 12,025
  • 12
  • 86
  • 145
Zig
  • 2,603
  • 1
  • 14
  • 20
  • 1
    I also cleared Derived data, and cleaned the project, don't know if necessary for everyone but here it seemed to be. Thanks – Simon Oct 16 '17 at 18:49
  • 3
    Hi @Zig. A repo I use on github (mattdonnelly/swifter) is at Swift 4.0, but has obviously been compiled using XCode 9.0, so I'm getting this problem. What are the steps needed to recompile someone's else cocoapod on Github for XCode 9.1 and above? – Joseph Beuys' Mum Dec 21 '17 at 12:15
  • 1
    is there a way to add command line tool? I just have Xcode 9.3 in my command line dropdown. and I have this problem too – tara tandel Apr 30 '18 at 07:04
  • 1
    Some of my pods aren't ready for Swift 4.2, hence I had to pin the `Swift Language Version` to `Swift 4` for those. Then it worked. – skofgar Sep 14 '18 at 16:13
  • 1
    Plus 1 for the shortcut for cleaning the build folder--wasn't something I knew how to do offhand. – Matt Mar 26 '19 at 20:57
  • I'm having this issue after upgrading to Xcode 10.2 (which introduces Swift 5). My build settings still say "Swift 4.2" and I rebuilt all my Carthage/Cocoapods dependencies, but the issue persists... – Nicolas Miari Apr 04 '19 at 01:41
  • Worked for me with Xcode 11 with error (swift 5) and swift 5.1 – blacker Sep 26 '19 at 23:17
  • After updating Xcode to 12.5 I got the error in my Swift 5 project for a Swift 4 libary (Cocoapods) that's been working fine. There's no update for it, so I just cleaned the project and now the error's gone again. – Neph May 04 '21 at 14:27
27

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.

Stunner
  • 12,025
  • 12
  • 86
  • 145
15

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.

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 -> Preferences -> Location -> XCode command Line Tool

Xcode Commend Line Tool

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

MatterGoal
  • 16,038
  • 19
  • 109
  • 186
Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
9

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Tina
  • 312
  • 3
  • 5
  • After 2 days of searching and trying every possible solution with carthage update, bootstrap, swift language settings etc. I found the right answer here :) – memres Aug 22 '19 at 08:27
7

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

Go to YourTarget > Build Settings > Swift Language Version for all of your Xcode 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's Swift Language Version to Swift 4.2, which eliminated the error.

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

Community
  • 1
  • 1
pkamb
  • 33,281
  • 23
  • 160
  • 191
3

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
user3288482
  • 49
  • 1
  • 2
1

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

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

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

wzso
  • 3,482
  • 5
  • 27
  • 48
0

In my case, I re-compiled the framework with whatever version it was complaining about.

To recompile, run pod update or update the carthage (which ever one you're using), and build the project.

Then search for the framework in finder. Copy-paste the theFramework.framework to your project and make sure to "Copy items if needed."Build the project again and it should work.

Joseph Francis
  • 1,111
  • 1
  • 15
  • 26
0

I fixed this with a few basic steps

  • Close Xcode
  • pod deintegrate
  • Open Xcode workspace
  • Command + Shift + K - Clean
  • Command + B - Build
Marquis103
  • 395
  • 3
  • 8