What’s New in Swift 5?

Swift 5 is finally available in Xcode 10.2! This release brings ABI stability and improves the language with some long-awaited features. See what’s new! By Cosmin Pupăză.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Using Variadic Parameters for Enumeration Cases With Associated Values

You can use variadic parameters for enumeration cases with associated values in Swift 4.2:

enum BlogPost {
  case tutorial(_: String...)
  case article(_: String...)
}

You use String… for tutorials and articles details. This isn't possible anymore in Swift 5, so you should use arrays instead:

enum BlogPost {
  case tutorial([String])
  case article([String])
}

You use [String] to set tutorials and articles details this time.

Deprecating String Index Encoded Offsets

Swift 4.2 strings are encoded using UTF-16. As a result encodedOffset returns an offset into the UTF-16 string:

let swiftVersion = "Swift 4.2"
let offset = swiftVersion.endIndex.encodedOffset

Here you get the offset of endIndex in swiftVersion. This doesn’t work for the UTF-8 string encoding employed in Swift 5, so Swift 5 replaces encodedOffset with utf16Offset(in:) to handle both cases [SE-0241]:

let swiftVersion = "Swift 5"
let offset = swiftVersion.endIndex.utf16Offset(in: swiftVersion)

New Pointer Methods

Swift 5 adds withContiguousStorageIfAvailable(_:) to Sequence and withContiguousMutableStorageIfAvailable(_:) to MutableCollection to provide generic implementations for withUnsafeBufferPointer(_:) and withUnsafeMutableBufferPointer(_:) in protocol extensions [SE-0237].

SIMD Vector Updates

Swift 5 adds operations on SIMD types for processors to the standard library. They provide low-level support for SIMD vectors and matrices. They also simplify the Objective-C, C and C++ implementations of <simd/simd.h> [SE-0229].

Where to Go From Here?

You can download the final playground using the Download Materials link at the top or bottom of this tutorial.

Swift 5 adds many cool features to Swift 4.2 and makes the language ABI stable. This is an important milestone in the language’s evolution, since there will be fewer changes from now on.

You can read more about the changes in this version of Swift on the official Swift CHANGELOG or the Swift standard library diffs.

You can also check out the Swift Evolution proposals to see what changes are coming in the next Swift version. Here, you can offer feedback for current proposals under review and even pitch a proposal yourself!

What do you like or dislike about Swift 5 so far? Let us know in the forum discussion below!

Cosmin Pupăză

Contributors

Cosmin Pupăză

Author

Sarah Reichelt

Tech Editor

Ryan Dube

Editor

Martín Riera

Illustrator

Marin Bencevic

Final Pass Editor

Richard Critz

Team Lead

Over 300 content creators. Join our team.