Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
Ilias Karim Avatar asked Feb 11 '20 20:02

Ilias Karim


People also ask

What version of Swift does Xcode use?

This book describes Swift 5.7, the default version of Swift that's included in Xcode 14. You can use Xcode 14 to build targets that are written in either Swift 5.7, Swift 4.2, or Swift 4. When you use Xcode 14 to build Swift 4 and Swift 4.2 code, most Swift 5.7 functionality is available.

How do I change the Swift language?

The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 4.0, 4.2, 5.0. This setting can be set in the build settings editor. #3932.


1 Answers

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.

enter image description here

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.

like image 74
byaruhaf Avatar answered Sep 27 '22 18:09

byaruhaf