How to migrate projects to Swift 4

Published Sep 12, 2017Last updated Oct 13, 2017
How to migrate projects to Swift 4

Swift 4 was released on 5th June 2017, with the key focus on ABI stability for the standard library and reflection enhancements.

While building on the strengths of Swift 3, Swift 4 mode ensures a higher level of stability and source code compatibility with Swift 3 as well.

New Features in Swift 4

  • Developers implemented enhancements to reflection, Dictionary and Set types’ creation and management;
  • Smart key paths for type-safe key value coding for Swift types were added
  • Exclusive memory access was enforced;
  • Developers simplified and accelerated the use of Strings;
  • Extension of the support of archival and serialization;
  • Enables type-safety for serialization to formats like plist and JSON.

Find more information about new features in Swift 4.

Source Compatibility Modes

Swift 3 set up the language for source-level stability and now Swift 4 delivers source compatibility as a feature moving forward. With Swift 4, you don’t have to modify any parts of code to use a new compiler version.

You can use the new Swift 4 compiler with three different modes:

1). Swift 3 version is the default for current code and will make source code that built with the Swift 3 compiler.

2). Swift 4 is a version making available the new and disruptive changes in the release. Of notable importance is an overhaul of the String API, where the main focus is made on the enhancement of API ergonomics.

These changes are source-breaking, and thus there is the need in existing code to migrate to using the new APIs.

Swift 4 allows using new features and performance optimizations of Swift 4, some of which can require migration, making it more simple and convenient to migrate comparing to project migration from Swift 2.2 to Swift 3.

One should note that the difference between Swift 3 and Swift 4 code is smaller than between Swift 2.2 and Swift 3 (in terms of migration issues).

3). A mixed mode enables iOS developers to benefit from interoperability between binaries made with the same compiler. It helps those programmers who have projects which mix packages written with Swift 3 with packages written with Swift 4, as long as all packages are built using the Swift 4 compiler. It enables you to piece by piece migrate code parts to Swift 4.

Also, learn more about version compatibility.

Migrating the project to Swift 4

Make sure that the project you want to migrate, builds successfully in Swift 3.2. Keep in mind that there are significant differences between Swift 3.1 and Swift 3.2 modes, so your project may have some errors (for example, if it’s built in Swift 3.1, not 3.2) that required to be solved before starting the migration.

Take into account that by making an iOS project managed under source control you'll get the ability to easier review the changes that were made through the migration assistant as well as to discard them and restart the migration process if necessary.

Find out other useful tips on project migration to Swift 4.

Discover and read more posts from Diana
get started