17

Apparently I have managed to build my project in Xcode 9 beta and now I only get the error

Module compiled with swift 4.0 cannot be imported in swift 3.1

When I run the project in Xcode 8. The module in my case are Alamofire. I have tried to restart Xcode but nothing happens any ideas how to solve this issue?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • 1
    Note: had same issue and Clean & Rebuild my project fixed the problem, I didn't have to edit Podfile – Hussein Aug 06 '17 at 07:36

6 Answers6

24

You have two options that you can do:

Clean the project and then try to re-build your solution and see if it works.

If it don´t work and you still get the same error message then do the following steps and it should work for you:

  1. Open your podfile and remove Alamofire
  2. Run pod update
  3. Re-add Alamofire to your podfile
  4. Run pod update
  5. When this is done, clean your project and run it
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Same problem here but using Carthage. Trying all the tricks I know to clean project and the problem still here. Any idea how to solve it for Carthage? – Dominique Vial Aug 02 '17 at 03:15
  • followed the steps, but unfortunately didn't work. Interesting fact is that it works on the simulator (same iPad version as hardware version) without a problem, but on preparing for the hardware iPad it shows this error (Alamofire 4.5.1) – Heki Oct 29 '17 at 07:54
  • @Heki, try to remove the app from your device and reinstall it and see if it makes any difference. – Rashwan L Oct 29 '17 at 08:00
  • @Rashwan L, unfortunately the app never made it to the device so I cannot remove it. The build failes – Heki Oct 30 '17 at 10:43
19

Same problem here but using Carthage. And here is the answer:

  • rm -rf ~/Library/Caches/org.carthage.CarthageKit/DerivedData
  • delete the Carthage folder for the project
  • Update Carthage: carthage update --platform iOS

And voilà!

Dominique Vial
  • 3,729
  • 2
  • 25
  • 45
5

I had the same problem and cleaning the build folder helped:

Command+Option+Shift+K

or

Product -> Option+Clean

2

Just deleting Derived data worked for me, no need to do Pod install again

Harsha
  • 717
  • 1
  • 11
  • 23
0

I met this problem in a project where dependency is managed by Carthage. In my case, I didn't set command line tool in xcode (Type in xcodebuild -version, you will know whether you set it up or not), so first step is to go to XCode --> Preference --> Locations then select the xcode you want to serve as command line tool. Then you can follow the steps that @Domsware mentioned above to rebuild all frameworks you are gonna use.

===============================================

Same problem here but using Carthage. And here is the answer:

rm -rf ~/Library/Caches/org.carthage.CarthageKit/DerivedData
delete the Carthage folder for the project
Update Carthage: carthage update --platform iOS

===============================================

Then don't forget to delete old links under 'Linked frameworks and libraries' and drag all frameworks from /Carthage folder under you project to 'Linked frameworks and libraries'.

Then voilà!

For those who are using CocoaPods, I suspect (Disclaimer: I didn't encounter this problem in project where CocoaPods is the dependency manager) the solution would be run the following command in terminal:

$ pod deintegrate
$ pod clean
$ pod install

where you might need to install 'deintegrate' and 'clean' tool for CocoaPod

$ sudo gem install cocoapods-deintegrate cocoapods-clean

more details see post How to remove CocoaPods from a project?

infinity_coding7
  • 434
  • 5
  • 16
0

Add following lines at the end of your pod file:

post_install do |installer|
    print "Setting the default SWIFT_VERSION to 4.0\n"
    installer.pods_project.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '4.0'
    end

    installer.pods_project.targets.each do |target|
        if ['SomeTarget-iOS', 'SomeTarget-watchOS'].include? "#{target}"
            print "Setting #{target}'s SWIFT_VERSION to 3.0\n"
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.0'
            end
        else
            print "Setting #{target}'s SWIFT_VERSION to Undefined (Xcode will automatically resolve)\n"
            target.build_configurations.each do |config|
                config.build_settings.delete('SWIFT_VERSION')
            end
        end
    end
end
Shubham Mishra
  • 1,303
  • 13
  • 24