How to Switch to Swift 4.0 in Xcode 9.3

How can I switch to Swift 4.0 in Xcode 9.3?

Make sure you have updated xCode then under Build Settings, check the drop down options for Swift Compiler - Language.

Furthermore,
You can download Xcode 8.x and 9.x from Apple Download Portal, if you've premium developer account (apple id). You can install & work with both Xcode 9 and Xcode 8.x in single (mac) system. (Make sure you've Command Line Tools supporting both version of Xcode, to work with terminal)

Is it possible to use Swift 4.0 (4.0.3) with XCode 9.3?

It's not possible.

But you can have two different Xcode versions in the Applications folder, just rename one of them.

Swift build settings in Xcode to use lower 4.0.3 version instead of 5.0.1 are not enforced

SWIFT_VERSION (Swift Language Version) key does nothing with an actual Swift version of the resulting binary. From $ swift --help:

-swift-version Interpret input according to a specific Swift language version number

So, basically, Xcode executes Swift compiler with -swift-version 4 argument. It changes how Swift compiler parses the source files, but do not affect the binary in any way.

To build the actual Swift 4 binary, you have to install the Swift 4 toolchain. Then you would be able to pick the correct toolchain from the menu Xcode -> Toolchains. But, honestly, I wouldn't recommend doing this way, because Apple does not care about older versions of toolchains, and Xcode usually behaves unstable with them.

Instead, I'd recommend you install older Xcode (in your case 9.3) from the Apple Downloads website and build the project from there.

Alternatively, you can set up relatively simple CI/CD inside Github Actions, CircleCI or TravisCI and build in multiple Xcode versions at the same time there. And use the latest Xcode locally. Because they these CIs provide you multiple virtual environments with different Xcode and macOS versions.

How can I choose Swift compiler version

Originally I posted this is a comment, but I should have just posted it as an answer:

If you're writing an app for the App Store, you can only use a release (non-beta) version of Xcode (see “Submitting Apps to the App Store using Xcode”) and the toolchain supplied with that version of Xcode (see “Using Downloads / Apple Platforms”). So you can either use Xcode 7.2 and continue using Swift 2.1, or you can use Xcode 7.3 and update your code to Swift 2.2.

You can have multiple versions of Xcode installed. You can keep Xcode 7.2 installed and use it for your non-updated Swift 2.1 projects, and use Xcode 7.3 on new projects. Note that you'll have to manually open each project in the appropriate version of Xcode. You can download old versions of Xcode here.

If you're not going to put your app in the App Store, then maybe you could extract the Swift 2.1 toolchain from the Xcode 7.2 bundle and turn it into a .xctoolchain for use with Xcode 7.3, but you're really in unexplored, unsupported territory if you go that route.

Xcode 9.2 is not showing Swift 4.1

Apple documentation was incorrect, Swift documentation is correct.

Well, to be double sure of which version is supported, we'll test it:

  1. Set your command-line tools to xcode 9.2 (9C40b). It will switch to Xcode default swift version.

    sudo xcode-select --switch /Applications/Xcode.app
  2. Test your swift version.

    swift --version

Result is:

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)

In conclusion, it's really an Apple documentation issue:

Swift 4.1 is only natively available using Xcode 9.3+



Update: 3 months later

With the release of Xcode 10 on June 4th 2018, Apple documentation was finally correctly updated, stating that Swift 4.2 is available using Xcode 10.0+.

Module compiled with Swift 4.0 cannot be imported in Swift 4.0.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.

What versions of Swift are supported by what versions of Xcode?

Since I've been gathering data and doing tests, I'll post my results as an updated chart in this answer:

A chart depicting the different versions of Swift as compared to their respective versions of Xcode. Last updated 2020-02-25

Awhile ago, I found out that newer versions of Xcode do not, in fact, support migrating from all older versions of Swift. I did explicitly test that Xcodes 10.2 through 11 don't support Swift 3.x and earlier, so I colored those white. I've not yet had time to test Xcode 8.3 through 10.1, but I suspect they will migrate 3.x but not 2.x or earlier; that's why there's a big "Unknown" block at the top.



Sources

  • Manual testing with this test code: https://github.com/BenLeggiero/Swift-Version-Checker
  • Xcode Release Notes
  • Swift Release Notes

Property setter called instead of getter in switch (Xcode 9.3 & 9.4)

For those facing the same problem, do not use get set when handling with Realm. Realm access properties through KVO, so in some situations it gets lost and cause this kind of error. Use get-only properties combined with functions to set this properties instead using get set



Related Topics



Leave a reply



Submit