What Is the "Swift Language Version" Xcode Setting For? Because It Still Builds Newer Swift Code with an Older Version Set

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

Prior to Swift 4, the version of the compiler and the language were one and the same. But since Swift 4, the compiler can run in a compatibility mode for previous Swift versions. check more info on compatibility modes in the Swift 4.0 release notes

The Xcode build setting SWIFT_VERSION set's the compiler flag -swift-version which is the language mode. From the swift compiler print out below this parameter only changes how the input is interpreted.

swiftc -h|grep 'Swift language version number'
-swift-version <vers> Interpret input according to a specific Swift language version number

Thus When you select Swift Language Version to 4.2, this does not mean use Swift 4.2 compiler. The compiler version will still be 5.1.3, the Swift Language Version setting instructs the compiler to run in Swift 4.2 compatibility mode. The compatibility mode means you may not need to modify your swift 4.2 code to use the new version of the compiler. Because the compiler running in compatibility mode allows Swift version 4.2 code to compile and run alongside code from version 5 and later.

compiler options

The Swift 5 compiler with compatibility mode can compile code written with either Swift 4 syntax, Swift 4.2 syntax, or Swift 5 syntax.

Here is a code example, create a file test.swift with code below:

//code written before siwft 5
let firstName = "michael jackson"
let offset = firstName.endIndex.encodedOffset

// Check swift version being used.
#if swift(>=5.2)
print("Hello, Swift 5.2")

#elseif swift(>=5.1)
print("Hello, Swift 5.1")

#elseif swift(>=5.0)
print("Hello, Swift 5.0")

#elseif swift(>=4.2)
print("Hello, Swift 4.2")

#elseif swift(>=4.1)
print("Hello, Swift 4.1")

#elseif swift(>=4.0)
print("Hello, Swift 4.0")

#endif

suppose the above code was written before swift 5 using the swift 4 compiler
this code will compile with no error's as shown below.

Sample Image

After swift 5 is released if you try to compile this code with Swift 5 compiler as shown below.

Swift 5 compiler

You will get the warning shown above since encodedOffset is deprecated in swift 5.

You could downgrade and use the swift 4 compiler or you can use the Swift 5 compiler in compatibility mode with the compiler flag -swift-version as shown below.

Swift 5 compiler with compatibility mode

It's important to note that Swift 4 compiler, and the Swift 5 compiler in Swift-4 compatibility mode are not the same thing. New swift 5 language features are normally available to the swift 5 compiler running compatibility mode. This allows developers to use the new features even when they can't upgrade to swift 5. The new Swift 5 features will not be available to the Swift 4 compiler.

The “Swift Language Version” (SWIFT_VERSION) build setting error with project in Objective C

For Objective C Projects created using Xcode 8 and now opening in Xcode 9, it is showing the same error as mentioned in the question.

To fix that, Press the + button in Build Settings and select Add User-Defined Setting.

Then in the new row created add SWIFT_VERSION as key and 3.2 as value like below.

It will fix the error for objective c projects.

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift.

Xcode 8 seems to be wrongfully displaying a nonexistent ("phantom", if you will) No. There's a trivial fix for this:

  1. Click the tiny arrow next to SWIFT_VERSION to toggle the drop-down list.
  2. Set the values for Debug and Release to Yes.
  3. Set the values for Debug and Release back to No.

The image below was taken just after setting Debug to No. Xcode shows that both Debug and Release are set to No, however SWIFT_VERSION reads <Multiple Values>:

Sample Image

From this, I can only conclude that it is a bug; and that some value (or more probably a null value) is being misrepresented as No.

The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift

The error is pretty clear.

  • In the project navigator select the (blue) project
  • Select the target which uses Swift
  • Click on Build Settings
  • Scroll down to Swift Language Version or in the search field type Swift L
  • In the popup of the setting select the language version

How do I see which version of Swift I'm using?

Project build settings have a block 'Swift Compiler - Languages', which stores information about Swift Language Version in key-value format. It will show you all available (supported) Swift Language Version for your Xcode and active version also by a tick mark.

Project ► (Select Your Project Target) ► Build Settings ► (Type
'swift_version' in the Search bar) Swift Compiler Language ► Swift Language
Version ► Click on Language list to open it (and there will be a tick mark on any one of list-item, that will be current swift version).

Look at this snapshot, for easy understanding:

xcode with described areas highlighted


With help of following code, programmatically you can find Swift version supported by your project.

#if swift(>=5.7)
print("Hello, Swift 5.7")

#elseif swift(>=5.6)
print("Hello, Swift 5.6")

#elseif swift(>=5.5)
print("Hello, Swift 5.5")

#elseif swift(>=5.4)
print("Hello, Swift 5.4")

#elseif swift(>=5.3)
print("Hello, Swift 5.3")

#elseif swift(>=5.2)
print("Hello, Swift 5.2")

#elseif swift(>=5.1)
print("Hello, Swift 5.1")

#elseif swift(>=5.0)
print("Hello, Swift 5.0")

#elseif swift(>=4.2)
print("Hello, Swift 4.2")

#elseif swift(>=4.1)
print("Hello, Swift 4.1")

#elseif swift(>=4.0)
print("Hello, Swift 4.0")

#elseif swift(>=3.2)
print("Hello, Swift 3.2")

#elseif swift(>=3.0)
print("Hello, Swift 3.0")

#elseif swift(>=2.2)
print("Hello, Swift 2.2")

#elseif swift(>=2.1)
print("Hello, Swift 2.1")

#elseif swift(>=2.0)
print("Hello, Swift 2.0")

#elseif swift(>=1.2)
print("Hello, Swift 1.2")

#elseif swift(>=1.1)
print("Hello, Swift 1.1")

#elseif swift(>=1.0)
print("Hello, Swift 1.0")

#endif

Here is result using Playground (with Xcode 11.x)

Sample Image

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.

Swift Language Version' option is missing in Xcode 9.1 Build Setting

You appear to have an Objective-C project rather than a Swift project. Objective-C projects do not have a Swift Language Version setting as a default.

You can add Swift Language Version as a User-Defined Setting, as described here.



Related Topics



Leave a reply



Submit