How to set Swift version 5 (for recent dev snapshots) in Xcode build settings?

(Ankit Aggarwal) #5

I thought of two ways to get around this limitation but they don't work because of build system issues:

  • Adding -swift-version 5 in OTHER_SWIFT_FLAG doesn't work because these flags are added before the build system adds the evaluated value of SWIFT_VERSION.
  • Overriding SWIFT_VERSION using an xcconfig doesn't work because the build system validates the value against the hardcoded list.

Swift 5.0 Nightly Development Snapshots!
(Ankit Aggarwal) #6

The list of values in SWIFT_VERSION is effectively hardcoded so you don't see the language version 5 in the list. It is still possible to set a custom value using an xcconfig file (see my previous post). The radar could be for providing an easy way to configuring arbitrary values for SWIFT_VERSION or for reading the list of supported language versions from the compiler in use. /cc @NeoNacho

(Gwendal Roué) #7

Thank you, @Aciid.

This reveals an opportunity for improvement in the release process of snapshots. I don't know who is the DRI (directly responsible individual) of the integration of snapshots in Xcode, but I would like this person to know about this thread promptly. CC @tkremenek.

The number of developers who try snapshots should raise, if only for their feedback. For one @Jens who tells about his difficulties, how many silently move on to something else?

(Jens Persson) #8

Would you mind going through step by step exactly how you got this to work?

I've added a file SetSwift5Hack.xcconfig:


And set it to be used on both Debug and Release, both project and target level, but it does not have any effect on eg this program:

func test() {
    #if swift(>=3.0)

    #if swift(>=4.0)

    #if swift(>=4.2)

    #if swift(>=5)

which will always print


(if 4.2 is what the Swift Language Version of my target is set to, leaving it unspecified is not supported by Xcode) no matter what value I set for SWIFT_VERSION in the xcconfig file (tried "5", 5.0, 4, etc).

Adding an intentional syntax error like an exclamation mark before SWIFT_VERSION will have Xcode complain about it so at least it doesn't completely ignore the file. Any ideas?

(Note that the above test program does print >=5 if I use the swiftc (of a recent dev snapshot) from the command line.)

(Ankit Aggarwal) #9

Did you remove the existing entry from the build settings editor? Go to your target's build setting editor, select the SWIFT_VERSION row and press delete key (do the same for project level setting in case it is also explicitly set). You can check the build logs to confirm which version was used. Expand the "Compile Swift sources files" and look for the -swift-version argument in the command-line.

(Jens Persson) #10

Hmm, doing that (for both the target and the project) will show "Swift 5 (unsupported)" in the combo box (which seems a bit promising, sort of). But when I try to compile Xcode will complain:

error: SWIFT_VERSION '5' is unsupported, supported versions are: 3.0, 4.0, 4.2. (in target 'Blabla')

Setting SWIFT_VERSION in my xcconfig file to 4 and 4.2 will work and set the version accordingly (I can verify it by the output of the above test program). Commenting out the SWIFT_VERSION line will result in Xcode telling me:

error: Value for SWIFT_VERSION cannot be empty. (in target 'Blabla')

(Ankit Aggarwal) #11

That means you're still using an older version of compiler. Are you sure you selected the dev toolchain? You can check in logs which compiler is being used.

(Jens Persson) #12

I have the same dev snapshot toolchain selected in Xcode as I have working with Swift 5 from the cmd line:

AFAICS the log ends with the above before it has had any chance of saying anything about which compiler it uses.

(Ankit Aggarwal) #13

Ok, it looks the build system always validates the value of SWIFT_VERSION against the hardcoded list so there is no way make it work with the latest version of Xcode. I'll go ahead and file some radars. We definitely need some solution here :/

(Jens Persson) #14

Thank you!

Just out of curiosity, what environment are people using for eg trying out Swift 5?

I get the feeling I'm the only one trying to use Xcode for this, and everyone else is using some much better setup, how else to explain the fact that this thread seems to be the only place on the internet that talks about this problem?

Or is almost no one trying out Swift 5 (on larger projects, with code completion, etc)?

(Ankit Aggarwal) #15

Are there really a lot of changes that you get only in the 5.0 mode? IIRC there were less changes this time. You can still use the Swift 5 snapshots in Swift 4.0 and 4.2 modes.

(Jens Persson) #16

I’m expecting a lot of bug fixes in Swift 5.

(Ankit Aggarwal) #17

Most of the time you will see the fixes across all language versions unless it is going to present a source compatibility issue.

(Jens Persson) #18

Well, here's the most recent example that I know of where the behavior is different between Swift 5 and 4.2 (at least in recent snapshots), repeated here for clarity:

func hmm(_ fn:  (Int, Int)  -> Int) { print("two ints") }
func hmm(_ fn: ((Int, Int)) -> Int) { print("pair") }

func test() {
  let twoIntsFn: (Int, Int) -> Int = { $0 + $1 }
  let pairFn: ((Int, Int)) -> Int =  { $0.0 + $0.1 }

  hmm(twoIntsFn) // error with both Swift 5 and Swift 4.2
  hmm(pairFn) // OK with Swift 5, error in Swift 4.2

All I was trying to say was that I'm a bit surprised at how uncommon an activity it seems to be to try out recent snapshots and switch between an upcoming Swift version and the current one, at least from within Xcode.

(Slava Pestov) #19

You can still try out the Swift 5 snapshots with the older language modes, and this is still extremely valuable for us because it can catch regressions.

(Ted Kremenek) #20

Here is a hack that works for trying out the snapshots in Swift 5 mode:

  1. Edit the file /Applications/

  2. Search for SupportedLanguageVersions and add 5.0 as a valid value.

Plan for NIO 2 and Swift 5
(Ted Kremenek) #21

It's a good observation. Xcode puts in reasonable guard rails so uses of the production compiler stay within expected operating parameters. It's something we can potentially look to being more capability inferred in Xcode based on the compiler used, rather than a hardwired set of valid language versions.

(Adrian Zubarev) #22

Not sure if it‘s even related, but in the meantime maybe this small tool can help? I have nit tried the latest snaprahots myself, but will probably do on friday.

(Jens Persson) #23

No, not with the issue discussed in this thread, ie not being able to use swift version 5 (for recent snapshots) in Xcode 10.1.

The solution was presented by @tkremenek above.

(Leo) #24

thanks, @tkremenek. I wrote a whole post about it here.
It seems you can use the Legacy Build System since that seems to ignore SWIFT_VERSION . Besides being Legacy what other issues could that raise? Is the New Build System in Xcode just more optimized?