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?
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.
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.
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.
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.
After swift 5 is released if you try to compile this code with Swift 5 compiler as shown below.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With