Swift 5.5: New Features for Concurrent Programming and Other Updates

9 months ago   •   6 min read

By Anastasiya Dumyak

Each WWDC conference introduces us to tons of innovations across numerous Apple products. Of course, as a mobile app development company, our team could not let the news pass by. We even gathered in our office to watch it so we could share our thoughts right away.

Previously, we discussed news about iOS 15 features. But now it’s time to dive deep into a more techy side of the conference — Swift 5.5.

This release is likely the most serious we have seen of Swift’s upgrades. We were introduced to completely new features, as well as lots of minor changes to pre-existing elements. Let’s dig in and review some of them.

Swift Concurrency

Swift 5.5 is packed with updates for making programming easier. And async/await, which we have been waiting for, is among the most exciting ones. This new feature makes an asynchronous code run just like it would as a synchronous one.

Writing and executing code, block after block, does not work for every project: you might need to run two blocks simultaneously or have a more significant time gap between them.

These scenarios are when asynchronous and concurrent programming comes in to save you time while making the process more manageable. An asynchronous function does not use any resources when suspended, so Swift can reuse a previously running thread at another time.

How can you apply these methods in your code? By using async and await keywords. First, you mark your functions with the async keyword and then call it with the await one. Async and await functions are syntactically similar to the familiar throws and try ones, so you can use them in pairs.

(Source: swift.org)
(Source: swift.org)

Even more, if you can call both synchronous and asynchronous functions, Swift will decide for you based on which suits the context better.

(Source: swift.org)
(An asynchronous sequence example. Source: swift.org)
(A structured concurrency example. Source: swift.org)
(An unstructured concurrency example. Source: swift.org)

Understanding concurrency in Swift is more than a few paragraphs. For instance, you might have noticed that there are different types of it: structured and unstructured. To guide you further, we recommend watching WWDC sessions dedicated to this topic, starting with this one.

Actors

Another big addition in Swift 5.5 are Actors, which is a whole new notion. What are these? Actors are reference types, like classes, designed for a concurrent. In other words, it is a multi-thread environment. If you can’t tell already, this Swift release is a lot about concurrency.

When putting any data into actors, you should state that you expect it to be accessed concurrently and want the process to be coordinated in a certain way. Actors help you prevent data from being inconsistent or corrupted when shared between two or more threads and, as a result, you can avoid serious bugs.These advantages are all thanks to the way actors work. Only one thread at a time can access a mutable state inside an actor and voilà: any action that can cause data corruption is suspended.

(Source: swift.org)

A nice touch is that actors work seamlessly with async and await functions. For instance, by marking a certain method with the await function, you can suspend it to wait for the network operation. At the same time, other methods that do not depend on that operation can use the very same actor without waiting or corrupting the data.

(Source: swift.org)
(The code that’s part of the actor doesn’t write await when accessing the actor’s properties. Source: swift.org)

Swift Package Collections

Using Swift packages is becoming easier — no more endless googling to find the right one. Swift Package Collection is a curated list of Swift packages that you can use either from the command line or Xcode 13. All the packages are JSON files that can be published basically anywhere.

(Source: Swift.org)

The great news is anyone can create a curated list of packages, so it is possible to make your own collection for any need or situation. You can also search for existing packages; there is a chance someone has already prepared a list for your case.

New Swift Algorithms

In Swift 5.5, there is a wider variety of algorithms with around 40 new ones added in the recent update. While Swift creators point out that learning the vocabulary might take a bit of effort, it is definitely worth it. Using Swift algorithms can save you significant time when generating element combinations or selecting the elements that suit your specifications.

(Source: Apple)
(An example of a Swift algorithm in use. Source: Apple)
(An example of a Swift algorithm in use. Source: Apple)
(An example of a Swift algorithm in use. Source: Apple)

Extended property wrappers

Property wrappers help you apply common semantics to properties. And from now on, they can be used as the parameters for functions and closures. But the parameters remain immutable, so you can still access the underlying property wrapper type via a leading underscore.

How does this innovation help? It is easier to get rid of redundancy in code and creates simpler APIs overall.

(Source: Apple)

Swift Type Checker update

The Swift team worked hard to improve the performance of Type Checker, adding updates that will make your coding process a bit easier. Now, you will see fewer “expression too complex” errors. Additionally, the process of checking array literals types will be faster.

(Source: Apple)

Speeding up incremental builds

There have been three major changes focused on increasing the speed of incremental builds. First, incremental imports are now supported, meaning the system will not rebuild each source file that imports a module whenever this module changes.

Second, the module dependency graphs are now computed upfront. Why? Because it allows you to start incremental builds of only what has changed.

Finally, selective recompilation now works with extensions so there are fewer recompilations where you have to change the body of the extension.

Results: Сhanges for the better

As we mentioned, the main emphasis of Swift 5.5 is concurrent programming. These efforts are to make your app more responsive and, as a result, bring a better user experience. But it will definitely take some time to digest all the changes as there is a lot to learn and memorize. The introduction of async/await and actors is a massive innovation in itself.

Let’s remember, no updates are ever intended to make our lives more complicated, only easier. So, at Perpetio, we are ready to spend the time it takes to perfect Swift’s new functionality and make our iOS apps even more convenient. What about you?








Spread the word

Keep reading