What is the “Swift Language Version” Xcode setting for? Because it still builds newer Swift code with an older version set

I'm building source code using an Xcode project that has its "Swift Language Version" set to "Swift 4." Even with this set, the project builds Swift 5.1 source code like, for example, implicit returns and Swift 5 source code using the new `isMultiple(of:)` method.



The Swift 5 and 5.1 code still works even with "Swift Language Version" set to "Swift 4" or "Swift 4.2"



It's only when running `#elseif swift(>=4.1)` statements that there appears to be a difference (code from https://stackoverflow.com/a/46080904/414415). Those results output their expected results.



However, I'm left wondering, how does the my Swift 5 source code compile successfully when the build setting clearly states Swift 4? And if the setting does not change which version of Swift I can use, what does it actually do?

Now that the Swift standard library is built in to the system, the availability of routines like

isMultiple(of:)
is controlled by the system availability mechanism, not the Swift language version.

Also, now that Swift is generally source stable, the Swift language version is a lot less relevant. These days you mostly see it used as a compatibility measure. For example, if there’s some bad thing that the compiler used to allow but no longer does (because it’s bad), the code that rules it out may trigger a warning if the Swift language setting is old but an error if the Swift language setting is new.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The Swift *language* version controls what syntax the compiler accepts, wherever the syntax changed between 4.2 and 5. In fact, there were very few syntax changes, so code written according to Swift 5 rules will likely compile in Swift 4 mode without errors. Similarly, there weren't very many syntax changes between 4 and 4.2.


However, there are a probably a few changes in Swift 5 where the syntax didn't change, but the meaning of the syntax changed. In that case, your code would still compile without error in all modes, but might do something different at run time.


The right thing to do here, when you open a project set to Swift 4 mode in a newer Xcode that supports version 5, is to run the code conversion tool to find any syntax you need to fix, and to mark the project as using version 5.


If you're writing new code using Swft 5 language rules, then don't try to compile it in Swift 4 mode, even if it seems to work.

What is the “Swift Language Version” Xcode setting for? Because it still builds newer Swift code with an older version set
 
 
Q