This year’s “What’s new in Swift” included a plethora of improvements and new add-ons. Some of the news are:

  • better diagnostic compiler error’s and hence better debugging
  • improved and faster code completion
  • improved auto indentation
  • improved integration
  • improved handling of chained method calls and property accesses
  • A standardized way to delegate a program’s entry point with the @main attribute
  • Swift Numerics, an open source project bringing additional support for numeric functions to Swift
  • Swift Argument Parser, build your command line tools with Swift
  • where clauses on contextually generic declarations
  • Multi-Pattern catch clauses
  • Float16

But the list is much longer. In the following are some highlights for what is to come with Swift 5.3 and you can find a list of further reading and related videos at the bottom of the post.

Additional Cross Platform Support and Swift AWS Lambdas

To support the Swift ecosystem Apple has added additional cross-platform support and will officially support:

  • All Apple platforms
  • Ubuntu 16.04, 18.04, 20.04
  • CentOS
  • Amazon Linux 2
  • Windows (coming from Swift 5.3)

This also opens up for official support for Swift AWS Lambdas. The AWSLambdaRuntime package is based on SwiftNIO and allows for both closure based Lambdas and eventloop Lambda functions. Below is an example of a closure based Lambda as presented in the talk:

import AWSLambdaRuntime
Lambda.run { (_, name: String, callback) in
    callback(.success("Hello, \(name)!"))
}

Enum Improvements

This year we get two big improvements to the enum type:

Synthesized Comparable Conformance For enum Types

With Swift 4.1 came synthesized, opt-in enum conformance for Equatable and Hashable, however conformance to Comparable was left out. From Swift 5.3 Comparable will also be included, allowing for easy sorting by the order of declaration. The synthesized, opt-in conformace only comes for enums without raw values, without associated types or with comparable associated types.

enum ApplePlatform: Comparable {
    case iOS
    case ipadOS
    case macOS
    case tvOS
    case watchOS
}

[ApplePlatform.watchOS, ApplePlatform.ipadOS, ApplePlatform.macOS].sorted()
// [ApplePlatform.ipadOS, ApplePlatform.macOS, ApplePlatform.watchOS]

Enum Cases As Protocol Witnesses

Another great improvement to enums in Swift is the possibility to allow for static protocol requirements to be witnessed by an enum case. It introduces two rules:

  1. static, get-only protocol requirement can be witnessed by an enum case without an associated value
  2. static func requirement can be witnessed by an enum case with one or more associated values
protocol ErrorReportable {
    static var notFound: Self { get }
    static func invalid(searchTerm: String) -> Self
}

enum UserError: ErrorReportable {
    case notFound
    case invalid(searchTerm: String)
}

Multiple Trailing Closures

If you have been following along in the discussions on Swift forums, you may have come across the topic of multiple trailing closures. It has been thoroughly discussed and many people had their opinions on both the syntax and the final process.

Up until now it has only been possible to use a trailing closure if your method has one escaping closure. An example of that is one of the animate methods in UIKit

import UIKit
UIView.animate(withDuration: 0.3) {
    self.view.alpha = 0
}

If we instead wanted to use the animate method that also allows us to define what should happen at completion we can’t use trailing closures anymore

import UIKit
UIView.animate(
    withDuration: 0.3, 
    animations: { self.view.alpha = 0 },
    completion: { _ in 
        self.view.removeFromSuperview()
    } 
)

With Swift 5.3 any method can now have multiple trailing closures. The first closure will be without a label, while all remaining closures should be labeled. The animate method from before will now look like:

import UIKit
UIView.animate(withDuration: 0.3) { 
    self.view.alpha = 0 
} completion: { _ in 
    self.view.removeFromSuperview()
}

Increased Availability of Implicit self in Closures

Swift requires the explicit use of self in escaping closures which capture it. This can lead to a lot of repetitive code. With implicit self in closures in Swift 5.3, we can omit all self. inside our closures. Instead we can add self to the capture list of the closure. Using implicit self will then update our animate method from before, so we only have to specify self once in each closure.

import UIKit
UIView.animate(withDuration: 0.3) { [self] in
    view.alpha = 0 
} completion: { [self] _ in 
    view.removeFromSuperview()
}

Other WWDC Articles

Resources and Further Reading

Article Photo - capture from What’s new in Swift by Apple

Author

Heidi Puk Hermann

Vapor Developer

👩🏼‍💻 Physicist turned teacher turned developer... Passionate about learning and teaching

You may also like

Engineering Awesome Conference 2020

We love engaging with the tech community, and we are big consumers of the awesome work that people share online. So a couple of weeks ago we hosted our first-ever online conference as a way to give back to the community. 6 experts from across our offices in Europe and...

AFK
API First

We’ve been building APIs for a long time, enabling our mobile software solutions to communicate with each other across their respective platforms. For years these APIs have been built as part of a “mobile first”-approach, under the guiding light of an internally defined, living document, our so called “API Manifesto”,...