Xcode logo Swift

Migrating to Swift 5

Swift 5 has been released! In this post I will walk you through some tips for migrating your project from Swift 4 to Swift 5.

Xcode 10.2

First, start by updating your Xcode to 10.2. If you cannot update all your projects at the same time, it could be wise to duplicate your Xcode and rename it to Xcode (insert your version here). When you start updating Xcode, you will still have the previous Xcode too!

Note: If you want to develop on iOS 12.2 on Xcode 10.1, you can do so by:

  1. Close all Xcode applications
  2. Navigate to your Xcode 10.2 application location
  3. Secondary click and select “Show Package Contents”
  4. Navigate to “Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport”
  5. Copy the folder “12.2”
  6. Follow step 1 to 3 but now for Xcode 10.1
  7. Paste the folder you copied on step 4
  8. Restart Xcode 10.1 and you should now be able to build on iOS 12.2!

Swift 5

When you open Xcode 10.2 for the first time on an existing project, you might see a warning from Xcode that suggests conversion to Swift 5 is available. If not, you can select “Edit -> Convert -> To Current Swift Syntax…”. If you use more than 1 target or if you’re using CocoaPods with non Swift 5 frameworks, you need to select the targets you want to convert to Swift 5. Make sure you have only checked your targets and frameworks you control or else the conversion could fail because Xcode could not edit a framework. Xcode will now try to convert your code to Swift 5 and will show you the changes for each file it wants to do. You can choose to ignore these changes or accept them.

Xcode Convert To Current Swift Syntax Failed error message.

If you do get an error even though your project builds, no worries! We can resolve this by navigating to our project settings and select the tab “Build Settings”. Search for “Swift Language Version” and select Swift 5 from the dropdown menu:

Build Settings menu displaying the Swift Language Version dropdown menu.

You can do this for every target you want to convert to Swift 5 (I recommend to update to Swift 5 for all your targets). Now when you compile, Xcode will check your code show warnings/errors for API changes and come up with suggestions. Now the painful part, we have to resolve every single issue because Xcode could not do it for us. Luckily there is an amazing shortcut: CMD + Option/Alt + CTRL + F or select “Editor -> Fix All Issues”. This will automatically fix all issues, in the current file, and apply what Xcode is suggesting to use.

Editor menu screenshot showcasing the Fix All Issues option.

For more information about migrating to Swift 5 visit: https://swift.org/migration-guide-swift5/

Dependencies

Visit the GitHub repos of your dependencies and check if they are already busy making a Swift 5 version or if they already released it. If neither, make an issue if there is not one already, or even better, convert the dependency yourself and make a pull request!

Do not worry if some dependencies are Swift 5 and some are not, they should work in perfect harmony together since Xcode 10. If you are using CocoaPods you might notice that every time you use “pod install” the Swift Language Version in the Build Settings are stuck on the version you had before. This could be Swift 4, 4.2 or 5. You can resolve this using the “post_install” in your Podfile:

DEFAULT_SWIFT_VERSION = '4.2'
SWIFT_VERSION_MAP = {
  'AnOldFramework' => '4.0',
  'ANewFramework' => '5.0'
}

post_install do |installer|
  installer.pods_project.targets.each do |target|
    swift_version = SWIFT_VERSION_MAP[target.name] || DEFAULT_SWIFT_VERSION
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = swift_version
    end
  end
end