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ă.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
What’s New in Swift 5?
25 mins
- Getting Started
- Language Improvements
- Testing Integer Multiples
- Escaping Raw Strings
- Using New Character Properties
- Using New Unicode Scalar Properties
- Removing Subsequences
- Dictionary Updates
- Compacting Dictionaries
- Renaming Dictionary Literals
- Numeric Protocol Updates
- String Interpolation Updates
- Handling Future Enumeration Cases
- Adding Result to the Standard Library
- Conforming Never to Equatable and Hashable
- Dynamically Callable Types
- Swift Package Manager Updates
- Platform Deployment Settings
- Target Build Settings
- Dependency Mirroring
- Miscellaneous Bits and Pieces
- Making Codable Ranges
- Flattening Nested Optionals
- Removing Customization Points From Collections
- Identity Key Paths
- Initializing Literals Through Coercion
- Build Configuration Updates
- Using Variadic Parameters for Enumeration Cases With Associated Values
- Deprecating String Index Encoded Offsets
- New Pointer Methods
- SIMD Vector Updates
- Where to Go From Here?
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!