Swift 4 Release & Overview: How to successfully migrate to Swift 4
Tags: swift, ios, iosdev
Published: 2017-09-08 11:07:02

---
layout: post
title: "Swift 4 Release & Overview: How to successfully migrate to Swift 4"
published: true
tags: swift, ios, iosdev, techtalks
cover_image: "https://thepracticaldev.s3.amazonaws.com/i/ydgxklox13ezzd2xfeu7.jpg"
---

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 </span><a href="https://swift.org/blog/swift-4-0-release-process/"><span style="font-weight: 400;">Swift 4</span></a><span style="font-weight: 400;">, 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. 



<h3>What’s new in Swift 4</h3>
 

- 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



<h3>Swift 4 Migration Changes Overview</h3>


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.


<b>SDK Changes</b>

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.


<b>Notable Special Cases</b>


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(()).



<b>Adding tuple destructuring</b>

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) }



<b>Default parameter values must be public</b>


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.
&nbsp;

<h3>Project Migration to Swift 4</h3>
 

- 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.
&nbsp;

Find out more <b></span><a href="https://smartym.pro/blog/swift-4-release-how-to-migrate-your-project-to-swift-4-programming-language/"><span style="font-weight: 400;">useful tips for a successful migration to Swift 4.</b></span></a><span style="font-weight: 400;"> 

Also, learn more about </span><a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Compatibility.html"><span style="font-weight: 400;">version compatibility.</span></a><span style="font-weight: 400;">