Taking advantage of Swift 5.4 changes in SwiftUI Development

Sai Durga Mahesh
3 min readMay 1, 2021
Photo by Jungwoo Hong on Unsplash

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.

@resultBuilderstruct CombinedTextViewBuilder {
static func buildBlock(_ texts: Text...) -> Text {
var final: Text = Text("Joined Text 👉🏻") for part in texts { final = final + part } return final
}
}

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.

@resultBuilderstruct CombinedTextViewBuilder {  static func buildBlock(_ parts: Text...) -> Text {    var final: Text = Text("Joined Text 👉🏻")    for part in parts {      final = final + part    }    return final  }  static func buildOptional(_ component: Text?) -> Text {    if let c = component {      return c    } else {      return Text("Unknown")    }  }  static func buildEither(first component: Text) -> Text {    component  }  static func buildEither(second component: Text) -> Text {    component  }  static func buildArray(_ components: [Text]) -> Text {    var final: Text = Text("Joined Text 👉🏻")    components.forEach { part in      final = final + part    }   return final  }}

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

Let’s use this in swiftui view

struct Test12_5: View {  var array: [String] = ["","love"]  @CombinedTextViewBuilder var textViewComplex: Text {    Text("Test 1")    Text("Test 2")    if Bool.random() {      Text("test if")    } else {      Text("test else")   }
for arrayElemnt in array {
Text(arrayElemnt) } } var body: some View { VStack { textViewComplex } }}

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.

VStack {
....
}.accentColor(.black.opacity(.zero))

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.

@CombinedTextViewBuilder func buildText(numbers: Int..., strings: String...) -> some View {   for i in numbers {     Text("\(i)")   }   for i in strings {     Text(i)   }}

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

struct Test12_5: View {  var array: [String] = ["","love"]  @CombinedTextViewBuilder var scrollviewComplex: Text {     Text("test 1")     Text("test 2")     if Bool.random() {       Text("test 3")     }     for arrayElemnt in array {       Text(arrayElemnt)     }  }  @CombinedTextViewBuilder func buildText(numbers: Int..., strings:
String...) -> some View {
for i in numbers { Text("\(i)") } for i in strings { Text(i) } } var body: some View { VStack { buildText(numbers: 5,4, strings: "hi","hello") }.accentColor(.black.opacity(.zero)) }}

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

Happy Development::))

--

--

Sai Durga Mahesh

Using Data Science to provide better solutions to real word problems