Taking advantage of Swift 5.4 changes in SwiftUI Development

Swift 5.4 and XCode 12.5 brought us some new features. With the latest version of Xcode, we can record videos and make animated GIFs. You can know the complete process here. Also with Swift 5.4 you can make functions with multiple variadic parameters and can overload functions locally.

In this article we will take the few advancements in swift 5.4 and apply in SwiftUI development.

ResultBuilders

ResultBuilders are used to build the new value with a customisable sequence.

During development with SwiftUI, I feel I could combine some of my Text Views and get a new combined Text View. This can be done with help of ResultBuilders with ease.

In above snippet:

‘@resultBuilder’ tells that following is a result Builder. buildBlock is a function that must be implemented. It takes series of text views and combine them to a single Text View.

With the result builders we can do even more things. some of them are using if else conditions to generate sequence and using array to form a sequence. This all can be done by implementing more functions in above snippet.

buildEither functions make if else conditions possible and buildArray helps to make sequence using array.

Let’s use this in swiftui view

We made a combined Text View with sequence of Text Views build from conditionals and array. Thanks to result builders..

Improved Implicit Member Access

Swift 5.4 is capable of understanding complex multiple chained members. This can be really helpful for SwiftUI developers as they use chaining a lot. The following is an example.

Multiple Variadic Parameters

This feature combined with result builders can make more sense. With help of Multiple variadic parameters, we will build a result builder function.

We combined numbers and strings to create a Text View with help of result builders and multiple variadic parameters.

Also there are improvements of faster compilation and better error tracking which could help our development too.

Happy Development::))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sai Durga Mahesh

Sai Durga Mahesh

73 Followers

Using Data Science to provide better solutions to real word problems