What’s New in Swift 4.2?
Swift 4.2 is finally out! This article will take you through the advancements and changes the language has to offer in its latest version. 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 4.2?
30 mins
- Getting Started
- Language Improvements
- Generating Random Numbers
- Dynamic Member Lookup
- Enumeration Cases Collections
- New Sequence Methods
- Testing Sequence Elements
- Conditional Conformance Updates
- Hashable Improvements
- Removing Elements From Collections
- Toggling Boolean States
- New Compiler Directives
- New Pointer Functions
- Memory Layout Updates
- Inline Functions in Modules
- Miscellaneous Bits and Pieces
- Swift Package Manager Updates
- Removing Implicitly Unwrapped Optionals
- Where to Go From Here?
Miscellaneous Bits and Pieces
There are a few other changes in Swift 4.2 you should know about:
Swift Package Manager Updates
Swift 4.2 adds a few improvements to the Swift Package Manager:
Defining Swift language versions for packages
Swift 4.1 defined swiftLanguageVersions
in Package.swift as [Int]
, so you could declare only major releases for your packages:
let package = Package(name: "Package", swiftLanguageVersions: [4])
Swift 4.2 lets you define minor versions as well with SwiftVersion
cases [SE-0209]:
let package = Package(name: "Package", swiftLanguageVersions: [.v4_2])
You can also use .version(_:)
to declare future releases:
let package = Package(name: "Package", swiftLanguageVersions: [.version("5")])
Declaring local dependencies for packages
In Swift 4.1, you declared dependencies for your packages using repository links. This added overhead if you had interconnected packages, so Swift 4.2 uses local paths in this case instead [SE-0201].
Adding system library targets to packages
System-module packages required separate repositories in Swift 4.1. This made the package manager harder to use, so Swift 4.2 replaces them with system library targets [SE-0208].
Removing Implicitly Unwrapped Optionals
In Swift 4.1, you could use implicitly unwrapped optionals in nested types:
let favoriteNumbers: [Int!] = [10, nil, 7, nil]
let favoriteSongs: [String: [String]!] = ["Cosmin": ["Nothing Else Matters", "Stairway to Heaven"],
"Oana": nil]
let credentials: (usermame: String!, password: String!) = ("Cosmin", nil)
Swift 4.2 removes them from arrays, dictionaries and tuples [SE-0054]:
let favoriteNumbers: [Int?] = [10, nil, 7, nil]
let favoriteSongs: [String: [String]?] = ["Cosmin": ["Nothing Else Matters", "Stairway to Heaven"],
"Oana": nil]
let credentials: (usermame: String?, password: String?) = ("Cosmin", nil)
Where to Go From Here?
You can download the final playground using the Download Materials link at either the top or bottom of this tutorial.
Swift 4.2 improves upon many Swift 4.1 features and prepares the language for ABI stability in Swift 5, coming in early 2019.
You can read more about the changes in this version either 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 Swift 5. Here you can give feedback for current proposals under review and even pitch a proposal yourself!
What do you like or dislike about Swift 4.2 so far? Let us know in the forum discussion below!