Swift 5.5 is available, it handles concurrency better

Get a free Techzine subscription!

Apple has released version 5.5 of the Swift programming language, described as a ‘massive release’. It includes async/await pattern, package collections, and improved Objective-C interoperability. The most anticipated new features in Swift 5.5 revolve around concurrency.

Swift’s earlier approach to concurrency was that when several asynchronous operations were written by a developer, it created a ‘pyramid of doom’. The phrase describes nested calls that are hard to follow and that made flow control and error handling a problem. Swift’s natural error handling mechanism could not be used, according to the language’s inventor, Christ Lattner.

Solving the problem

The solution was to adopt the async/await pattern, as used in Microsoft’s C# (which drew inspiration from asynchronous workflows in F#) and adopted by other languages like Python and JavaScript.

The new concurrency model adopted will improve interoperability for Objective-C, an older Apple language. Objective-C has completion-handler methods, translatable into async methods in Swift, and equally Swift async methods are exportable as completion-handler methods.

A second crucial part of the Swift concurrency model is actors. Actors are a reference type for protecting access to Swift’s mutable state and are introduced with the keyword actor.

Explained

Because they are embodied by an internal queue abstraction, Lattner said that you communicate with actors asynchronously and actors guarantee that the data under their protection is in contact with code running on that queue. What that does is provide an ‘island of serialization’ in a ‘sea of concurrency.’

This massive release’s other big feature is package collections. They are a group of packages where a package is a set of Swift source code files and a manifest. They are supported by the independent Swift Package Index and integrated into Xcode 13, the new version released with the iOS 15 release and iPhone 13.