Swift Version Build Configuration

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.

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

In the navigator selection bar, click the magnifying glass, then search for "SWIFT_VERSION" You will find the places in the project where you can adjust the swift version accordingly.

Sample Image

Sample Image

“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.

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.

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

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.

How to change Swift version from 5 to 4 in Xcode?

Project ► (Select Your Project Target) ► Build Settings ► (Type

'swift_version' in the Search bar) Swift Compiler Language ► Swift

Language Version ► Click on Language list

Here you can change your version.

An example screenshot



Related Topics



Leave a reply



Submit