Migrating from Swift 4 to Swift 5

Swift 5 is finally here.

 ????It’s the next major release of Swift and it brings ABI Stability 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 PreparationBefore 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 migrator build 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 Edit Scheme sheet and select the Build 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 AssistantNow, when you open your project with Xcode 10.

2 for the first time , you will see a migration opportunity item in the Issue Navigator.

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 Edit menu.

Edit -> Convert -> To Current Swift Syntax…You will be presented with a list of targets to migrate.

Note: Targets that don’t contain any Swift code will not be selected.

When you click on the Next button, you should see the Generate Preview 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 Save to proceed.

This also changes your Swift Language Version build setting for your migrated targets to Swift 5.

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.

OverviewThe migration has minimal impact to code compiled with 4.

2 version.

However, you may encounter these in your own code :CompilerA compiler error, add () to forward @autoclosure parameter with a fix-it that is automatically applied by the migrator.

Type nullability mismatch errors.

A compiler warning, saying switch must be exhaustive.

The Swift Standard LibraryCompiler warnings 'index(of:)' is deprecated: renamed to 'firstIndex(of:)' and 'index(where:)' is deprecated: renamed to 'firstIndex(where:).

SDKFew APIs from AppKit changed their return type to Any or AnyObject instead of NSBindingSelectionMarker.

AVFoundation, CloudKit, and GameKit have a few properties whose return type became nullable.

Post Migration ShenanigansThe 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 Overview.

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,If you have any comment, question, or recommendation, feel free to post them in the comment section below!Enjoy!.????.

. More details

Leave a Reply