Migrating from Swift 4 to Swift 5

A “Swift” guide of migrating your code from Swift 4 to Swift 5.

Zahurafzal Mirza
Apr 11, 2019 · 4 min read
Image for post
Image for post

YAY! Swift 5 is finally here. 🎊

It’s the next major release of Swift and it brings at last. Besides this, several new key features are also added which include raw Strings, future enum cases, a Result type, checking multiple integers and more.

We will cover all the features about Swift 5 later in another article whose link I will provide soon enough. For now, we will talk about migrating our existing Swift 4 code to Swift 5.

The Pre-Migration Preparation

Before you start the migration to Swift 5 , make sure your project builds successfully in Swift 4 or Swift 4.2 and all the tests have passed. You may need to resolve some compiler-related errors initially though.

More importantly , it’s important to have your project under source control. This will easily allow you to review the changes that got applied via the migration assistant. Also if any thing came up you can always do the migration again if needed. It’s not an all-or-nothing process as targets in Swift4, Swift 4.2 and Swift 5 can co-exist and link together.

The migration assistant does a to gather the changes, using the scheme you have selected, so the targets that will get processed are the ones that are included in the scheme. To review and modify what is included in the scheme, invoke the sheet and select the tab from the column on the left, and make sure all your targets and their unit tests are included.

Now that we are all setup, let the migration begin!

Swift Migration Assistant

Now, when you open your project with Xcode 10.2 for the first time , you will see a migration opportunity item in the . Clicking on it will activate a sheet asking you if you’d like to migrate. You can either choose to be reminded later or invoke the migrator manually from menu.

Edit -> Convert -> To Current Swift Syntax…

You will be presented with a list of targets to migrate.

When you click on the button, you should see the sheet, and the migration assistant starts a migration build to get changes in your code.

After this is done, you’ll see all the changes, so click on to proceed.

This also changes your build setting for your migrated targets to .

Certain issues while processing the targets will negatively impact the migration process. Switch to the Report Navigator and check the conversion build log for any errors. If there are errors related to code-sign, try disabling the code-signing of the target.

Overview

The migration has minimal impact to code compiled with 4.2 version. However, you may encounter these in your own code :

Compiler

  1. A compiler error, add () to forward @autoclosure parameter with a fix-it that is automatically applied by the migrator.
  2. Type nullability mismatch errors.
  3. A compiler warning, saying switch must be exhaustive.

The Swift Standard Library

  1. Compiler warnings 'index(of:)' is deprecated: renamed to 'firstIndex(of:)' and 'index(where:)' is deprecated: renamed to 'firstIndex(where:).

SDK

  1. Few APIs from AppKit changed their return type to Any or AnyObject instead of NSBindingSelectionMarker.
  2. AVFoundation, CloudKit, and GameKit have a few properties whose return type became nullable.

Post Migration Shenanigans

Image for post
Image for post

The migrator will take care of many changes , but you may need to do some manual changes to be able to build the project after applying the changes.

Like you may need to apply some fix-its or change certain functions and their declarations as mentioned above in the . Also, you may need to edit certain third-party libraries and Pods as well.

But not to worry, they will just be minimal changes.

Yeehaw! You are now done with the migration. 🎉

And that’s it guys! You’re done.

Hope you guys enjoyed reading this post and please share and recommend it to our other fellow Swifters. Coz,

Image for post
Image for post

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Enjoy! 😎

The Create School

Unlock your potential to build products of tomorrow and…

Zahurafzal Mirza

Written by

I am an iOS Developer by day, gamer by night and a really happy to go down to earth person. Yea, even while Coding.

The Create School

Unlock your potential to build products of tomorrow and assist global tech firms succeed. The create school aims to break an obsolete education model by grooming engineers to solve real-world problems, align with the industry’s demands & create cutting-edge digital products.

Zahurafzal Mirza

Written by

I am an iOS Developer by day, gamer by night and a really happy to go down to earth person. Yea, even while Coding.

The Create School

Unlock your potential to build products of tomorrow and assist global tech firms succeed. The create school aims to break an obsolete education model by grooming engineers to solve real-world problems, align with the industry’s demands & create cutting-edge digital products.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store