DEV Community

Cover image for Swift 4 Release & Overview: How to successfully migrate to Swift 4
Diana Maltseva
Diana Maltseva

Posted on

Swift 4 Release & Overview: How to successfully migrate to Swift 4

Swift is an open-source statically typed programming language, introduced by Apple at 2014 WWDC. Though emerged 3 years ago, Swift fastly became the hottest language for iOS developers and their favorite.

According to Stack Overflow Developer Survey, Swift took the 1st place as the Most Loved Programming Language for 2015 and the 2nd place in 2016.

Noteworthy that the founders work on the language constant improvements and updates. The latest one, called Swift 4, came into life on 5th June 2017, with the main focus on ABI stability for the standard library.

While building on the strengths of Swift 3, Swift 4 language provides a higher level of stability and provides source code compatibility with Swift 3.

What’s more, now Swift development takes less time. Also, a new version adds such features as smart key paths and serialization and has a reduced size of app binaries.

What’s new in Swift 4

  • Enhancements to reflection, Dictionary and Set types’ creation, using, and manipulation
  • Adds smart key paths for type-safe key value coding for Swift types
  • Enforced exclusive memory access
  • Makes the use of Strings, which retain Unicode correctness, easier and quicker
  • Adds extended support of archival and serialization
  • Enables type-safety for serialization to formats like plist and JSON

Swift 4 Migration Changes Overview

The vast majority of changes that the Migrator provides comes from data generated by a comparison of both previous and current SDKs, that can drive renaming of identifiers and types.

There are several special provisions where the Migrator can safely perform straightforward mechanical changes.

SDK Changes

The two most prevalent SDK changes are moving global constants into static type properties and transforming string constants into Swift enumeration cases. These are handled automatically by the Migrator. There are also various type signature changes.

Notable Special Cases

String has new APIs in Swift 4, some of which now return Substring or String. To ease this transition, the Migrator will insert explicit initializer conversions when an API now expects a different type.

SE-0110: Distinguish between single-tuple and multiple-argument function types
f: (Void) -> ()

When using f: (Void) -> () for the type of a function argument, it is generally meant to be f: () -> (), so the Migrator will suggest you use this type instead. Otherwise, with the new rules in SE-0110 for Swift 4, you would need to call the function f as f(()).

Adding tuple destructuring

For code such as:

swift func foo(_: ((Int, Int) -> ()) {} foo { (x, y) in print(x + y) }

The Migrator must add explicit tuple destructuring to continue building in Swift 4, such as:

swift func foo(_: ((Int, Int) -> ()) {} foo { let (x, y) = $0; print(x + y) }

Default parameter values must be public

The compiler is now more strict about the accessibility of referenced, non-literal values used as default arguments for your public functions; they must also be public.

  • Make the referenced default values public.
  • Provide public functions which return a sensible default value.  

Project Migration to Swift 4

  • First, ensure that the project you’re going to migrate to Swift 4, builds successfully in Swift 3.2. You should also remember about significant differences between Swift 3.1 and Swift 3.2, so your project may have some errors (for example, if it’s built in Swift 3.1) that need to be solved before migrating.

  • Keep in mind that by making an iOS project managed under source control you'll be able to easier review the changes that were implemented through a migration assistant as well as to discard them and restart the migration process if necessary.
     

Find out more useful tips for a successful migration to Swift 4.

Also, learn more about version compatibility.

Oldest comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.