How to build Swift 3 project on Xcode 9?

2019-01-22 10:55发布

问题:

Xcode 9 Release Notes says that Swift 3 compilation is supported:

One compiler for Swift 4 and Swift 3, Swift 4 and Swift 3 targets can be compiled together in the same project.

I have installed Xcode 9 beta. But when I press Build it shows bunch of errors. Are some special preparations need to do when use Swift 3 in Xcode 9?

UPD:

Module compiled with Swift 3.1 cannot be imported in Swift 4.0

回答1:

Select the target, goto Build Settings > Swift Language Version:



回答2:

All the above answers are answering the wrong question. When using Xcode 9, you are using the Swift 4 compiler (even if you are using it to compile Swift 3 code) to fix this, recompile using the appropriate compiler/Xcode.

If you are trying to do this via commandline, you can use sudo xcode-select -switch to switch between xcode versions. Good luck!



回答3:

I had same issue - (I installed pod which swift version was 3x and my project's swift version was Swift 4. So I got so many compiler errors. Later I changed the Swift version form 4 to 3.2 and tried to build project, again I got compiler errors. I think that happen because of I run pod install while my project was in Swift 4.)

  • First you need to change the "Swift language version" from build settings.
  • If you have installed any pod then changing only "Swift Language Version" won't help. You may need to run pod install for the project.


回答4:

Xcode 9 needs the framework to be built with Swift 3.2 or higher. Go to https://github.com/emaloney/CleanroomLogger and click Download ZIP. Open the xcodeproj in Xcode 9. Press ⌘B. CleanroomLogger.framework turns from red to black. Drag the framework into your project. See related question: Realm issue with Swift 3.1



回答5:

Update for Xcode 10.2 and Swift 4.2

Swift 3, 4, and 4.2 targets can coexist and link together.

You decide when and if you’d like to migrate on a per-target basis when it makes sense for your project. While migrating to Swift 4.2 is definitely encouraged, it’s not an all-or-nothing process, as Swift 3, 4, and 4.2 targets can coexist and link together.

Different cocoapod and different project version can also coexist.

You wish to have different cocoapod version as compared to your project version then you should make following changes at the end of your pod file:

post_install do |installer|
    print "Setting the default SWIFT_VERSION to 4.2\n"
    installer.pods_project.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '4.2'
    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

Sometimes even after you run pod update your xcode forcefully update your cocoapod's project version, then in that case select cocoapod pod project and change the swift version in the build settings.