What’s New in Swift 5.5?

Nikunj Prajapati
Mindful Engineering
6 min readJun 24, 2021

Are you exited about what’s new in Swift 5.5, then take a look at this blog.

This blog is all about what’s new in Swift 5.5. This new version has lot of improvements like async/await, actors, and many more.

So, Here are major changes for Swift 5.5.

1. Async/await functions :-

  • SE-0296 introduces asynchronous (async) function into swift, that allows us to run complex asynchronous code easily. An asynchronous function or asynchronous method is a special kind of function or method that can be suspended while it’s partway through execution. for creating async function, we have to first mark our function with async keyword, after it for calling this async function mark function with await keyword.
  • Previously we are using Completion handlers for sending values back after a function returns, but they had tricky syntax, now with async function it’s easier.
  • Ex. Here I am fetching and showing posts using async/await function. I’m just showing random numbers. You can write some asynchronous code in place of random number generator code.
  • Output :-
  • Note : Try to call showPosts() function in async context.
  • In Addition async/await will work easily with try/catch, meaning that async functions and initializers can throw error if needed. But for that we have to take care for particular order of keywords, means keywords order is reversed between call site and function. When it comes to calling them the order of keywords is flipped to try await rather than await try.

2. Async/await Sequences :-

  • SE-0298 introduces ability to loop over asynchronous sequences of values using a new AsyncSequence Protocol. This AsyncSequence will help when you want to process values in a sequence as they become available rather then precomputing them all at once.
  • You can use AsyncSequence same as normal sequence, but your types should conform to AsyncSequence and AsyncIterator, and your next() method should be marked with async keyword.
  • Once you have your asynchronous sequence, you can use for await to loop over its values in an async context.
  • Ex. NumberGenerator is an AsyncSequence, using for await loop we can iterate over its value in an async context. It will generate number in multiply of 2.
  • Output :-

3. Effectful read-only properties :-

  • SE-0310 upgrades read-only properties to support the async and throws keywords, making them more flexible.
  • Ex. Here I am checking is there any file named “dummy” in our Bundle using async and throws read-only properties.
  • Output :-

4. Structured Concurrency :-

  • SE-0304 introduces a wide range of approaches to executes, cancel and monitor concurrent operations.
  • The simplest async approach introduced by structured concurrency is the ability to use the @mainattribute to go immediately into an async context, which is done simply by marking the main() method with async, like this:
  • Ex. In below example we are fetching weather data according to location. I am just returning dummy data.
  • The main changes in Structured concurrency are backed by two new types, Task and TaskGroup. These two new types allows us to run concurrent operations either individually or in a coordinated way.
  • You can start concurrent work by creating a new Task object and passing it the operation you want to run. This will start running on a background thread immediately, and you can use await to wait for its finished value to come back.
  • Task provides built-in priorities of high, default, low and background. Task also provides us some useful static methods like sleep(), cancel(), etc. to control the way our code runs. You can also use Result with Task.
  • For complex work you should create TaskGroup with the help of withTaskGroup() function.
  • Ex. Here is a TaskGroup example which has three task.
  • Output :-

5. async let binding :-

  • SE-0317 introduces the ability to create and await child tasks using the simple syntax async let. This is for alternative to TaskGroups where you are dealing with heterogenous result types. i.e., if you want tasks in a group to return different kinds of data.
  • Note: You can only use async let if you are already in an async context.
  • Output :-

6. Actors :-

  • SE-0306 introduces actors, which is a new type. Actors are reference types. Unlike classes, actors allow only one task to access their mutable state at a time, which makes it safe for code in multiple tasks to interact with the same instance of an actor.
  • You introduce an actor with the actor keyword, followed by its definition in a pair of braces.
  • Ex. Here is a TemperatureChecker Actor which is checks weather.
  • Output :-

7. Global actors :-

  • SE-0316 allows global state to be isolated from data races by using actors.
  • Although there can be many global actors, the main benefit at least right now is the introduction of an @MainActor global actor you can use to mark properties and methods that should be accessed only on the main thread.

8. Sendable and @Sendable Closures :-

  • SE-0302 adds support for sendable data. Sendable data is a data that can safely be transferred to another thread. This is accomplished through a new Sendable protocol, and an @Sendable attribute for functions.
  • We can send many things safely to threads like, Swift’s core value types, including Bool,Int,String, and similar, Standard library collections, Tuples, etc.
  • Swift lets us use the @Sendable attribute on functions or closure to mark them as working concurrently.

9. #if for postfix member expressions :-

  • SE-0308 allows us to use #if conditions with postfix member expressions.
  • you can now optionally add modifiers to a view.
  • Ex.

10. Interchangeable use ofCGFloatandDoubletypes :-

SE-0307 introduces a important improvement now Swift is able to implicitly convert between CGFloat and Double in most places where it is needed.

Ex. Here I am doing addition of CGFloat and Double, the result of addition will be 80.0 which will be a Double type.

  • Output :-

11. lazy now works in local contexts :-

  • The lazy keyword allowed us to write stored properties that are only calculated when first used, but from Swift 5.5 onwards we can use lazy locally inside a function to create values that work similarly.
  • Ex. Below I have used lazy in local function.
  • Output :-

12. Extended property wrappers to function and closure parameters :-

  • SE-0293 extends property wrappers so they can be applied to parameters for functions and closures. Parameters passed this way are still immutable unless you take a copy of them, and you are still able to access the underlying property wrapper type using a leading underscore.

13. Extending static member lookup in generic contexts :-

  • SE-0299 allows Swift to perform static member lookup for members of protocols in generic functions.

That’s all for Swift 5.5.

--

--