What’s New in Swift 5.5?




579 Views

Swift is considered as one of the best ways to develop software for phones or desktops or anything which requires coding. It is one of the most interactive languages of all and is also fast and safe. It can be called as a combination of broad engineering culture of Apple and modern language along with a thorough and regular contribution by the community supporting it. As per Mobile App Development Company San Francisco, Swift is an industrial quality programming language and is quite friendly to the beginners.

MAJOR CHANGES IN SWIFT 5.5:

The new version of Swift, Swift 5.5 is here and has got many new improvements and functionalities. Below mentioned are some of the major changes brought in with the new version of Swift 5.5:

  1. Functionalities of Async/Await

Asynchronous function is introduced in Swift by SE-0296, which allows the complex codes easily. To create an async function one needs to first mark the function with async word and then to call it as async function, one needs to mark the function with the keyword, await. It is a special which can be suspended anytime if not required.

If one wants to understand how the Async function helps, then they need to analyze how the same problem was solved earlier. Completion handlers are very commonly used in the Swift which allows the user to send back the values after any function returns.

Below given are some rules which are to be followed while using the Swift:

  • Async functions can call all the other async functions as well as regular synchronous functions if required.
  • Swift will show an error, as synchronous functions cannot call the rest of async functions directly.
  • In case, there are both the functions, async and synchronous, which are to be called directly and in the same way, then Swift will prefer the one which matches the current context. Example, Swift will call the async if the call site is currently async and will call synchronous function in case of synchronous call site.

After addition of async functions and initializers, Swift can throw errors. But when it comes to calling then the keyword to be used will be ‘try await’ rather than ‘await try’. In Swift 5.0, the Result produced is not that relevant now as it has already improved the completion handlers. But still ‘Result’ can be called as the best method to store the result of any operation to evaluate the same in future.

2) Sequences Async/Await

Async sequence allows to loop over the asynchronous sequences of the values after introducing SE-0298. This function is really helpful when one wants to process the values in sequence as they become readily available other than pre-computing all of them at once.

The overall process is quite similar to using AsyncSequence. The only thing which is different is that the user types should conform to AsynchSequence and AsyncIterator and the ‘next’ method should be marked as sync.

Also, the user needs to make sure that they end back ‘nil’ from ‘next( )’ when it’s time to end the sequence.

Example- One can make a Double generator sequence which starts with 1 and doubles its number whenever it is called.

The protocol of AsyncSequence also helps with the auto implementation of different common methods like allSatisfy(), compactMap(), Map() etc.

3) Effectful read only properties

The read-only property of Swift is upgraded by introduction of SE-0310 which ultimately supports the ‘async’ and ‘throws’ keywords, may be jointly or individually that makes it significant and more flexible.

To explain it further, user can create Bundlefile Structure which loads the content of a particular in the application’s bundle of resource. The user can mark the ‘contents property’ as ‘async throws’ as the file may be or may not be there or if the file is there then it might not be readable or even if it is readable then there are possibility that it is quite big in size.

4) Structured concurrency

It facilitates the users to execute, cancel and monitor the concurrent operations in Swift by introducing SE-0304. It provides the ability to use the @main attribute to immediately get into an async context, and it is done by simply marking main() with async, this is the simplest approach introduced by the Structured concurrency.

Task and Task Group are the two types which support all the changes brought in by the Structured Concurrency. These types help to run the parallel operations in both the ways, individually as well as in a coordinated way.

Some of the in-built properties of the Task are default, high, low and background. It also helps with some static methods like cancel (), sleep () etc. which ultimately controls the process of coding. Also, Task facilitates to start a concurrent work by creating a new Task object and then sending it the operation as per the requirement. Task also helps with several static methods to control the way the code runs,

  • Calling Task.yield() will suspend the on-going current task for some time so that it gives time to some other tasks which might be in the waiting list.
  • Calling Task.sleep() will make the current task sleep for a specific number of seconds; till the time something better comes up.
  • Calling Task.checkCancellation() will check that if someone asked to cancel the task by writing cancel()  and if it is so then the user can throw, CancellationError.

Task Group is there to complete the complex tasks and it is created by using withTaskGroup() function.

5) Actors

Actors are the new type which is introduced through SE-0306. These are generally like the classes which are quite secure to use in the synchronous environments. Actors support removing the deadly bugs at the compiler level only as Swift ensures that mutable state inside the actor is accessed by a single thread only at a particular time.

The problems are resolved by Actors by using actor isolation which refers to the stored methods and properties which cannot be observed from outside until and unless they are performed asynchronously. Using the keyword actor, helps to create Actor. Usually, an Actor can use its own properties and methods freely but if it is interacting with another Actor, then it should be done asynchronously.

6) Async let binding

Here, using the simple syntax, async let, one can create and await the child tasks through SE-0317. Async let you create a User Interface from all of those values. If one is already in an async context, then the user can use ‘async let’.

7) Global Actors

With the help of Actors, SE-0316 allows to isolate global state from data races. This leads to formation of many global actors but by using keyword @mainactorglobal actor, one can mark those methods and properties which should be accessed only on the main thread.

CONCLUSION:

Other than the above-mentioned features, there are many more new things to explore in Swift 5.5 like -Interchangeable use of CGFloat and Doubletypes, #if for postfix member expressions, sendable and @sendable closures and many more. These updates show how hard the Swift team is working to deliver a good range of changes and that too in a short span of time.