Apple made Swift open-source in December 2015 and since then a new version of Swift getting released frequently. In these changing environment of Swift development, it’s essential to make a sure version of the Swift being used to build iOS apps are correct and as expected. We will cover how to automatically check the version of Swift using Fastlane plugin I wrote called ‘ensure-swift-version‘.
Why Check Swift Version
As per the Github releases on Swift repository, we can see that new DEVELOPMENT-SNAPSHOT being released almost every day. We can use Swift to different toolchain of the Swift easily to try out new features of the Swift language. There is information about the using custom toolchain on the Swift documentation page here.
We can check the version by running swift --version command from the terminal, however, we have to plug this step into our build process. Fastlane is one of the popular tool being used for build automation of the iOS apps. I thought it would be a great idea to add a plugin to Fastlane to check Swift version. Let’s see how to add a plugin to project use it.
Add Fastlane Support to Project
Let’s create a new Xcode project as single view iOS application. Let’s call it ‘Demo’
We can add Fastlane support to the project by creating Fastfile inside FastLane directory.
$ mkdir fastlane
$ touch fastlane/Fastfile
Add Fastlane Plugin
Assuming you already have Fastlane installed on your macOS, if not then follow the installation steps from documentation here. We can add FastLane plugin by running
$ fastlane add_plugin ensure_swift_version
This will setup Gemfile and Pluginfile for our project and install the plugin ‘ensure-swift-version‘.
Now that, we can use our newly added plugin. Let’s add a lane in our Fastfile
lane :swift do
ensure_swift_version(version: "Apple Swift version 3.1")
Here, we are checking that we are using our Swift version is 3.1. We can execute lane using
$ fastlane swift
As we can see, how easy it would be to plug this check into our build process. It’s ideal to use this check as part of the before_all lane so that we can perform this check before building and testing our app.
Hope you find this plugin useful.