80

After updating to Xcode 9, I tried to build one of my projects.

I use the FacebookLogin pod. I have a compiler error in FacebookLogin/LoginButton.swift

@testable import FacebookCore
❌ Module compiled with Swift 3.1 cannot be imported in Swift 4.0

In my target's build settings, the Swift language version is set to Swift 3.2.

Screenshot added

I guess I need to wait for Facebook to update their pod ? Or any other suggestion ?

Thanks !

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
Arnaud
  • 955
  • 1
  • 6
  • 15

14 Answers14

73

Update:

Solution also tested and working in Swift 5 and Xcode 11.

Original:

I would like to add that if you are using Carthage to compile a module in Swift 3.2 you should go to a terminal and run:

sudo xcode-select --switch /Applications/Xcode-beta.app/Contents/Developer

To use the Xcode 9 command line tools, then you can run:

carthage update NameOfTheLibrary --platform iOS --no-use-binaries

This will compile the library with your current command line tools, it can be a bit slow but now the project should build.

Note

To revert and use your stable Xcode command line tools simply run:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
xavi.pedrals
  • 1,466
  • 14
  • 13
  • 1
    I had the same error using Algolia framework, and build it with carthage and Xcode 9 command tool worked fine ! Thanks – vmeyer Aug 10 '17 at 10:14
  • 3
    Worked for me using SwiftyJson. Thanks! – leandrodemarco Aug 12 '17 at 18:22
  • 2
    Worked for me using Alamofire – pachun Sep 21 '17 at 18:41
  • Hi @xaviPedrals, I need your help... Im trying to run that project "https://github.com/swiftingio/SingTest" but getting that error "Module compiled with Swift 3.1 cannot be imported by the Swift 4.2.1 compiler: /Users/aliapple/Desktop/SingTestByAhtazaz/Pods/AudioKit/iOS/AudioKit.framework/Modules/AudioKit.swiftmodule/arm64.swiftmodule". Can you help me in this? – Ahtazaz Jun 17 '19 at 09:53
  • Hi @Mr.Ahtazaz that project is from 2 years ago so probably it was made and compiled in Swift 3, first the project should be updated to Swift 4 or 5, then try running the carthage command `carthage update NameOfTheLibrary --platform iOS --no-use-binaries` to update the libraries and you should be okay – xavi.pedrals Jun 17 '19 at 10:32
  • Thanks for the quick response @xaviPedrals. I have updated this project in swift 4.2. All things are Perfect but getting this ""Module compiled with Swift 3.1 cannot be imported by the Swift 4.2.1 compiler: /Users/aliapple/Desktop/SingTestByAhtazaz/Pods/AudioKit/iOS/AudioKit.framework/Modules/AudioKit.swiftmodule/arm64.swiftmodule"" ERROR on POD AudioKit. – Ahtazaz Jun 17 '19 at 10:35
  • @xaviPedrals I have got your instructions but dont know how to use carthage.. I just familiar with POds not carthage... Can you help me with that. I will be very Thankful to you Sir. – Ahtazaz Jun 17 '19 at 10:36
  • @vaviPedrals after running command on Terminal, got "-bash: carthage: command not found" – Ahtazaz Jun 17 '19 at 10:41
  • @xavi.pedrals i have update th eproject on latest Swift 4.2 but now only issue is coming on AudioKit... So how do i update AUDIOKIT Pod on latest version? – Ahtazaz Jun 17 '19 at 13:16
  • @xavi.pedrals Actually the problem is that, currently there are 3 PODS Installed in this project. pod 'TuningFork' pod 'UICircularProgressRing' pod 'ResearchKit' TuningFork depends on AudioKit & Chronos-Swift. So when i installed TuningFork, it automatically installs AudioKit & Chronos-Swift (but with old Language version). So, how do i update only AUDIOKIT & CHRONOS-SWIFT POD using TuningFork? – Ahtazaz Jun 17 '19 at 13:18
26

Xcode 9 comes with a Swift 4 compiler that understands both Swift 3.2 and swift 4, it even lets you mix and match between the 2 versions. Unfortunately, other versions aren't supported.

Even if you set your language to Swift 3.2, it uses the Swift 4 compiler.

If you're using cocoapods, you can add this to the end of your pod file to force the pods to use Swift 3.2 or 4.0:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.2'
        end
    end
end

Alternatively, you could put the files from the pod directly into your project temporarily, until FacebookLogin is updated to Swift 3.2 or 4.

Note: Edited based on Matt's feedback

klinger
  • 1,289
  • 1
  • 10
  • 24
  • 1
    FYI - This is somewhat overly aggressive by forcing all pods to Swift 4.0. If a pod is Swift 3 compliant, you can safely bump to Swift 3.2. Bumping to 4.0 will more than likely cause compiler issues for many pods. – Matt S. Jul 10 '17 at 04:47
19

Maybe you can clean the target before you build it. It works fine for me.

TomorJM
  • 197
  • 5
11

I ran into the same issue on Xcode 9 Beta 3, which pointing to 'Alamofire' and tried a few different solutions, the easiest one i found is

1. CMD+SHIFT+K to clean the build
2. Restart Xcode 9 <-- make sure you do this step, that's critical. `
Xu Yin
  • 3,932
  • 1
  • 26
  • 46
10

Doing a "clean build folder" and restarting Xcode 9 cleaned up the error for me. Also the error didn't stop the app from running on my device or the simulator.

John Pavley
  • 5,366
  • 2
  • 14
  • 16
  • 1
    "clean build folder": To access the option open the Product menu and use the Option key to show the Clean Build Folder option. – Hans Aug 18 '17 at 11:30
8

goto xcode DerivedData directory then remove all file inside it and recompile your project . it work for me .

and default DerivedData directory is :~/Library/Developer/Xcode/DerivedData.

reco
  • 305
  • 3
  • 10
8

If using Carthage , Open terminal and;

carthage update --platform iOS --no-use-binaries

If using Pod , Open terminal and;

pod update

(Also if don't work in pod, you can change SWIFT_VERSION in podfile Ex:

config.build_settings['SWIFT_VERSION'] = '3.2'

)

After;

Open Xcode and use;

Command+Option+Shift+K

enter image description here

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • 1
    Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, tailor your answers to the question. See https://meta.stackexchange.com/q/104227/311792 – Papershine Sep 14 '17 at 11:40
  • @paper1111 I explain everything in my answer and works – SwiftDeveloper Sep 14 '17 at 11:42
  • I'm using carthage and this is the solution that worked for me. carthage update "My Framework" did not cut it. I had to update the entire carthage catalogue. Took forever but did the trick. – Matan Guttman Sep 24 '17 at 07:22
  • 1
    This solution also worked for me. To be double sure make sure you close and relaunch xcode after cleaning the build folder with Command+Option+Shift+K. – Torsten Ojaperv Oct 18 '17 at 18:10
  • @SwiftDeveloper Where does the `config.build_settings` portion go? – Adrian Sep 29 '18 at 23:15
4

It works for me.

1.Clean your project in Xcode 8

2.Build or run your project in Xcode 9

RayJiang
  • 41
  • 2
3

I cleaned the project in Xcode 9, and then run the app, it works.

Shan Ye
  • 2,622
  • 19
  • 21
2

I had the same problem with Xcode 9 GM and this solved my problem: Remove it from the project and drag it again into "Embedded Binaries".

  • I had the issue with Alamofire 4.5.1 and tried everything else on this page via `pod update` but without result. In the end I added the framework directly to my project and added it to my Embedded Binaries which worked – Heki Nov 01 '17 at 06:48
2

Clean Build Folder

Cmd + option + shift + K
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
1

I have

pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'

in my project and import FBSDKLoginKit, after cleaning the target i didn't have any issue

Since the pod you are using is in swift and is a beta pod, it is likely that you would have some issues with the swift 4 compiler, you should use the objective-c version of the pod for the time being

thibaut noah
  • 1,474
  • 2
  • 11
  • 39
0

If you use from Pod:

  1. In Podfile comment FacebookLogin pod
  2. pod install

  3. In Podfile uncomment FacebookLogin pod
  4. pod install

  5. Run again your project
Hamed
  • 1,678
  • 18
  • 30
0

For my case - the actual pod referenced a static zip with prebuilt binaries targeting swift 3.1. So only solution is to rebuild the framework with source from xcode 9.

https://github.com/AudioKit/AudioKit/issues/980

johndpope
  • 5,035
  • 2
  • 41
  • 43
  • I have a similar issue with AudioKit being referenced inside pods. However, your solution refers to a build_frameworks.sh found in the 'Framework' Please add more details. I cannot find such a file. – TomV Oct 02 '17 at 13:50
  • try to point the podfile to a newer release. a version that's been built with latest xcode 9 - / no build script changes needed. https://github.com/AudioKit/AudioKit/releases – johndpope Oct 03 '17 at 16:12
  • Thanks for the tip. I had to update the AudioKit version in cocoapods to the latest one, which had the build_frameworks.sh. The build_frameworks.sh was needed to 'validate' AudioKit, to stop Apple rejecting the app. – TomV Oct 04 '17 at 14:26