Introduction
The Swift 5.3 version, extending language offers support across platforms like Windows and Linux distributions, has been one of the primary goals of this version, and Apple also has given a lot of focus on improving the overall language and its performance to boost SwiftUI and machine learning for iOS.
In this release following features are:
- Multiple Trailing Closures
- Multi-Pattern Catch Clauses
- Synthesized Comparable Conformance for Enums
- Enum Cases As Protocol Witnesses
- self Isn't Explicitly Required Everywhere
- A type-Based Program Entry point
- where Clauses on Contextually Generic Declarations
- New Collection Operations for Noncontiguous Elements
- A New Float16 Type
Multiple Trailing Closures
The SE-0279 proposal brings a new syntax for trailing closures that lets you call multiple closures as parameters of a function in a more readable way. This is a bit of syntactic sugar that lets you "pop" the final argument to a function out of the parentheses when it's closure.
Instead, it allows you to append several labeled closures after the initial unlabeled closure. The following example is demonstrated below:
- //Old Multiple trailing closures
- struct OldContentView: View {
- @State private var showOptions = false
- var body: some View {
- Button(action: {
- //write button action here
- }) {
- Image(systemName: "star")
- }
- }
- }
- //New Multiple trailing closures
- struct NewContentView: View {
- @State private var showOptions = false
- var body: some View {
- Button {
- // write button action here
- } label: {
- Image(systemName: "star")
- }
- }
- }
Multi-Pattern Catch Clauses
SE-0276 introduced the ability to catch multiple error cases inside a single catch block, which allows us to remove some duplication in our error handling.
Example
Catch clauses now allow the user to specify a comma-separated list of patterns with the ability to bind the variables with the catch body - like a switch statement.
- enum NetworkError: Error {
- case failure, timeout
- }
- //Old style declaration
- func networkCall(){
- do{
- try someNetworkCall()
- }catch NetworkError.timeout{
- print("timeout")
- }catch NetworkError.failure{
- print("failure")
- }
- }
- //new style declaration
- func networkCall(){
- do{
- try someNetworkCall()
- }catch NetworkError.failure, NetworkError.timeout{
- print("handle for both")
- }
- }
Synthesized Comparable Conformance for Enums
Until now, comparing two enums cases, you weren't straightforward. You'd have to conform to Comparable and write up a static fun < for determining if the raw value of case is lower than the other(or vice versa for >).
SE-0266 lets us opt into Comparable conformance for enums that either have no associated values or have associated values that are themselves Comparable.
An example of an enum that sorts them:
- enum Brightness: Comparable {
- case low(Int)
- case medium
- case high
- }
- ([.high, .low(1), .medium, .low(0)] as [Brightness]).sorted()
- // [Brightness.low(0), Brightness.low(1), Brightness.medium, Brightness.high]
Enum Cases As Protocol Witnesses
Swift had a very restrictive protocol witness matching model wherein writing enum cases with the same name and arguments as the protocol was not considered a match. We were forced to fall back on manual implementation instead.
Example

Join the conversation! Your thoughts help the community grow.