Transferring a project from Swift 4.2 to Swift 5.0

Hello, Habr! I present to you the translation of the article "Swift 5.0: How to migrate your project and frameworks" by Antoine Van Der Lee.

Swift 5.0 was released in March 2019 and is the first stable release of ABI Swift. Although many resources cover the new features of Swift 5.0, they do not often tell you what you need to do to upgrade your project to Swift 5.0.

In this post, I will cover the steps you can take to upgrade an existing project to Swift 5.0.

Verify that your project has already been ported to Swift 4.2.
I won’t be surprised if this article helps convince your product manager to plan the time for the transition.

Automatic migration using migrant assistant


Xcode suggests using the Migration Assistant to automatically update your code to the current Swift syntax. This conversion will use the latest version of Swift, available with the installed version of Xcode.

Xcode 10.2 is the first version to include Swift 5.0, so you need to use this version of the software.

This feature often does the hard work for you. Therefore, I recommend starting the code port by going to Edit -> Convert -> To Current Swift Syntax ....

Tip: Make sure that you only do this for your project and framework. You can skip conversion for any external dependencies.



Dependency Update


Most likely, you have some Swift dependencies that need to be updated. Many large open source projects, such as Alamofire and Moya, have already started working with version Swift 5.0.

However, it is likely that not all of your dependencies are already updated. Although I would like to urge you to carry out the transfer, and then submit the pull request yourself, you probably have to wait a bit until the project owners do it themselves.

If you plan to conduct a full intensive test of your application after this transfer, it may be worthwhile to also directly update your dependencies. Your test will cover these updates directly, which may serve as a “double win”.

CI Environment Update


If you use Travis, Jenkins, or any other CI platform, you also need to update Xcode.



Using the result in Swift


Xcode Converter performs only basic code changes. It does not account for the new Result type , which is now included in the Swift standard library. Many frameworks have included the "Result" type in their code. This may mean that you have a lot of data from the enumeration type ( an enum ), are no longer needed. However, do not rush to replace them with data from your dependencies: you depend on code changes in the external structure.

Common type of error



It is likely that your current “Result” type only defined the type for the appropriate case.

Result in Swift 5.0 also requires you to determine the expected type of error. As a respectable citizen, you should try to make this type of mistake specific based on what you expect. However, if you want to speed up the migration, you can also just set the type to Swift.Error .

Tip: If you want to know more about the new Result type in Swift 5.0, I would recommend you read this article .

We take into account the stability of ABI


In fact, ABI stability is a separate issue. You can read more about this on the Swift official blog.

Keep in mind that the size of the application you are downloading will decrease , because applications no longer need to embed the standard Swift library!

This should convince your product to find the time to transfer.
If you want to dive into Swift 5.0 more, you can skip to the Apple blog post “Migrating to Swift 5.0”.

Also popular now: