What’s New in Swift 5.1?
Swift 5.1 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 5.1?
25 mins
- Getting Started
- Language Improvements
- Opaque Result Types
- Implicit Returns From Single-Expression Functions
- Function Builders
- Property Wrappers
- Synthesizing Default Values for Initializers in Structures
- Self for Static Members
- Creating Uninitialized Arrays
- Diffing Ordered Collections
- Static and Class Subscripts
- Dynamic Member Lookup for Keypaths
- Keypaths for Tuples
- Equatable and Hashable Conformance for Weak and Unowned Properties
- Ambiguous Enumeration Cases
- Matching Optional Enumerations Against Non-optionals
- New Features for Strings
- Contiguous Strings
- Miscellaneous Bits and Pieces
- Converting Tuple Types
- Tuples with Duplicate Labels
- Overloading Functions With Any Parameters
- Type Aliases for Autoclosure Parameters
- Returning Self From Objective-C methods
- Stable ABI Libraries
- Where to Go From Here?
Overloading Functions With Any Parameters
Swift 5 prefers Any
parameters instead of generic arguments for overloads of functions with only one parameter:
func showInfo(_: Any) -> String {
return "Any value"
}
func showInfo<T>(_: T) -> String {
return "Generic value"
}
showInfo("Swift 5")
showInfo()
returns "Any value"
in this case. Swift 5.1 works the other way round:
func showInfo(_: Any) -> String {
"Any value"
}
func showInfo<T>(_: T) -> String {
"Generic value"
}
showInfo("Swift 5.1")
showInfo()
returns "Generic value"
this time.
Type Aliases for Autoclosure Parameters
You can’t declare type aliases for @autoclosure
parameters in Swift 5:
struct Closure<T> {
func apply(closure: @autoclosure () -> T) {
closure()
}
}
apply(closure:)
uses the autoclosure signature for closure
in this case. You may use type aliases in the prototype of apply(closure:)
in Swift 5.1:
struct Closure<T> {
typealias ClosureType = () -> T
func apply(closure: @autoclosure ClosureType) {
closure()
}
}
apply(closure:)
uses ClosureType
for closure
this time.
Returning Self From Objective-C methods
You have to inherit from NSObject
if your class contains an @objc
method which returns Self
in Swift 5:
class Clone: NSObject {
@objc func clone() -> Self {
return self
}
}
Clone
extends NSObject
because clone()
returns Self
. This is no longer the case in Swift 5.1:
class Clone {
@objc func clone() -> Self {
self
}
}
Clone
doesn’t have to inherit from anything this time.
Stable ABI Libraries
You use -enable-library-evolution
in Swift 5.1 to make changes to library types without breaking its ABI. Structures and enumerations marked as @frozen
can’t add, remove or reorder stored properties and cases [SE-0260].
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.1 adds many nice features to the ones already introduced in Swift 5. It also brings module stability to the language and implements complex paradigms used by the new frameworks introduced at WWDC like SwiftUI and Combine.
You can read more about the changes in this Swift version on the official Swift CHANGELOG or the Swift standard library differences.
You can also have a look at the Swift Evolution proposals to see what’s coming in the next version of Swift. Here, you can offer feedback for currently reviewed proposals and even pitch a proposal yourself!
What is your favorite Swift 5.1 feature so far? Let us know in the forum discussion below!