How to Compile Latest Swift Version on Older Xcode

Is it possible to compile Latest Swift version on older Xcode?

I figured it out that Yes it is possible. You can achieve it by installing the latest Snapshot:

Development Snapshots are prebuilt binaries that are automatically
created from mainline development branches.

https://swift.org/download/#snapshots

Snapshot is a toolchain that you can install into Xcode which contains the latest compiler, leading to let you able to build your app on the latest tools.


How to setup a snapshot into Xcode?

1- Download the latest snapshot (Xcode) from Swift website.

2- Install the downloaded snapshot .pkg file.

3- Run Xcode and then go to: Xcode -> Toolchains -> Manage toolchains.

4- After selecting "Components" - "Toolchains" options, you should see:

Sample Image

Select "Swift Developer Snapshot" and thats it! You might need to reset Xcode after doing the above steps.

Reminder: You should reselect "Xcode 8.3.3" option after done playing around Swift 4, as mentioned in Swift - Using Downloads section:

To submit to the App Store you must build your app using the version
of Swift that comes included within Xcode.

How can I choose Swift compiler version

Originally I posted this is a comment, but I should have just posted it as an answer:

If you're writing an app for the App Store, you can only use a release (non-beta) version of Xcode (see “Submitting Apps to the App Store using Xcode”) and the toolchain supplied with that version of Xcode (see “Using Downloads / Apple Platforms”). So you can either use Xcode 7.2 and continue using Swift 2.1, or you can use Xcode 7.3 and update your code to Swift 2.2.

You can have multiple versions of Xcode installed. You can keep Xcode 7.2 installed and use it for your non-updated Swift 2.1 projects, and use Xcode 7.3 on new projects. Note that you'll have to manually open each project in the appropriate version of Xcode. You can download old versions of Xcode here.

If you're not going to put your app in the App Store, then maybe you could extract the Swift 2.1 toolchain from the Xcode 7.2 bundle and turn it into a .xctoolchain for use with Xcode 7.3, but you're really in unexplored, unsupported territory if you go that route.

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

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.

How to build for newer iOS versions with outdated Xcode?

Yes. You can use iOS 14.7 on Xcode 12.4. You only need to add device support file in Xcode for iOS 14.7.

You need to add device support file from this link

Use older swift version

It would be easier to move forward and make the app compile with new version.

It's more likely to be as-> as! or as? which is mostly all the breaking changes for 1.2.

What is the correct way to update swift version in Xcode/Cocoapods project?

When updating Xcode version you should just be sure you don't end up in a situation where you can no longer work.

Instead of updating Xcode you could just install two version of Xcode on the same mac and use both of them by trying out the new version.

For now, you can go back to your previous Xcode by downloading the previous version here https://developer.apple.com/download/more, extract the archive you will download and copy the App file to your Applications folder in macOS.

What I would suggest as a solution before you start upgrading everything, as you don't know how all the new pod updates will work with your code implementation, is to delete the Xcode 10.2 and go back install 10.1 or 10.0 (can download them from the previous link).

For later on, when your project builds fine and it's stable and you would like to try to upgrade everything (Xcode, Swift and PODs), you should first of all check if your project build with your current Swift version (which Swift version is your project currently set on you can find under Target > Build Settings > Swift Language Version).

At this moment your project doesn't build, so make your project build by using a previous Xcode version to make your life easier and also to be sure that by upgrading everything you don't end up with broken functionality from other PODs that might have changed the way their implementation works with your project.

When your projects build without errors and you want to upgrade to a new version of Swift

  • Research on the latest version of your PODs and which Swift version they support (go the project github pages, or in cocoapods)
  • Try to upgrade just your project without upgrading your PODs, if that works you are going to upgrade also your PODs later on, to ensure your PODs stays with the current Swift version that is working for you (let's say it is right now Swift 3.2 for example) you add this snippet to your Podfile:

    post_install do |installer|
    installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
    config.build_settings['SWIFT_VERSION'] = '3.2'
    end
    end
    end
  • To convert just your project to the newer Swift version go to Edit -> Convert -> Convert to current swift syntax A popup will appear with a list of targets, including the pods. Deselect everything except your project target, the unit and UI tests and press convert. Wait for the project to build and generate the preview and apply the changes. Fix all the issues and warning created by the new Swift version requirements.

  • To update all the PODs that support the new Swift version you upgraded your project, do it by using the right POD version for each POD that has support to the newer Swift version and for the one that don't support yet the newer Swift version you could replace the code snippet on the Podfile with this

    post_install do |installer|
    installer.pods_project.targets.each do |target|
    if ['UnsupportedPod1', 'RxSwift', 'RxCocoa'].include? target.name
    target.build_configurations.each do |config|
    config.build_settings['SWIFT_VERSION'] = '3.2'
    end
    end
    end
    end

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.



Related Topics



Leave a reply



Submit