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:
-
- struct OldContentView: View {
- @State private var showOptions = false
-
- var body: some View {
- Button(action: {
-
- }) {
- Image(systemName: "star")
- }
- }
- }
-
-
-
- struct NewContentView: View {
- @State private var showOptions = false
-
- var body: some View {
- Button {
-
- } 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
- }
-
-
- func networkCall(){
- do{
- try someNetworkCall()
- }catch NetworkError.timeout{
- print("timeout")
- }catch NetworkError.failure{
- print("failure")
- }
- }
-
-
- 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()
-
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
- protocol DecodingError {
- static var fileCorrupted: Self { get }
- static func keyNotFound(_ key: String) -> Self
- }
-
- enum JSONDecodingError: DecodingError {
- case _fileCorrupted
- case _keyNotFound(_ key: String)
- static var fileCorrupted: Self { return ._fileCorrupted }
- static func keyNotFound(_ key: String) -> Self { return ._keyNotFound(key) }
- }
SE-0280 has lifted this restriction so that enum cases can be protocol witnesses if they provide the same case names and arguments as the protocol requires.
Example
- protocol DecodingError {
- static var fileCorrupted: Self { get }
- static func keyNotFound(_ key: String) -> Self
- }
- enum JSONDecodingError: DecodingError {
- case fileCorrupted
- case keyNotFound(_ key: String)
- }
Self Isn't Explicitly Required Everywhere
The SE-0269 proposal allows us to omit self in places where it's no longer necessary. Earlier, using self in closures was necessary when we were capturing values from the outside scope. Now when reference cycles are unlikely to occur, self would be implicitly present.
Example
- struct OldView: View {
-
- var body: some View {
- Button(action: {
- self.sayHello()
- }) {
- Text("Press")
- }
- }
-
- func sayHello(){}
- }
-
- struct NewView: View {
-
- var body: some View {
- Button {
- sayHello()
- } label: {
- Text("Press")
- }
- }
-
- func sayHello(){}
- }
The type-Based Program Entry point
SE-0281 introduces a new @main attributed that lets us define entry points for our apps. You needn't invoke an AppDelegate.main() method manually anymore, as marking struct or class with the attributed ensures it's the starting point of your program.
Example
- @main
- class AppDelegate: UIResponder, UIApplicationDelegate {
- static func main() {
- print("App will launch & exit right away.")
- }
- }
One can assume that the older domain-specific provided attributes, @UIApplicationMain and @NSApplicationMain, would be deprecated in the future versions in favor of @main.
However, there are some conditions you should be aware of when using @main :
- You may not use this attribute in an app that already has a main.swift file.
- You may not have more than one @main attribute.
- The @main attribute can be applied only to a base class - it will not be inherited by any subclasses.
New Collection Operations for Noncontiguous Elements
SE-0270 introduces a new RangeSet type that can represent any number of positions in a collection. This might sound fairly simple, but it actually enables some highly optimized functionality that will prove useful in many programs.
Example
- var numbers = Array(1...15)
-
-
- let indicesOfEvens = numbers.subranges(where: { $0.isMultiple(of: 2) })
-
-
- let multiplesofThree = numbers.subranges(where: { $0.isMultiple(of: 3) })
-
- let combinedIndexes = multiplesofThree.intersection(indicesOfEvens)
- let sum = numbers[combinedIndexes].reduce(0, +)
-
Using RangeSet, we can do a whole lot of computations and manipulations on collections. for example, by using the moveSubranges function, we can shift a range of indexes in an array.
- let rangeOfEvens = numbers.moveSubranges(indicesOfEvens, to: numbers.startIndex)
-
Refined didSet Semantics
SE-0268 adjusts the way the didSet property observers work so that they are more efficient. Thus doesn't require a code change unless you were somehow relying on the previous buggy behavior, you will just get a small performance improvement for free.
Internally, this change makes Swift not retrieve the previous value when setting a new value in any instance where you weren't using the old value, and if you don't reference oldValue and don't have a willSet, Swift will change your data in-place.
A New Float16 Type
SE-0277 introduced a new half-precision floating-point type called Float16, which is commonly used in graphics programming and machine learning, this new floating-point type fits in alongside Swift's other similar types:
Example
- let first: Float16 = 5
- let second: Float32 = 11
- let third: Float64 = 7
- let fourth: Float80 = 13