SwiftUI ViewBuilder is resultBuilder, and from Swift 5.3 you can declare if let, if case, multiple if statements, switch, ... pretty much like a normal block function.
ViewBuilder is recommended to leverage SwiftUI type system to ensure performance. Here are a few examples 👇
Local variables.
Pretty much like a normal function, you can declare local variables and return View at last.
Mix if and switch statements
This is handy when you want to show modal or overlay of different Views
If statement only.
No more if else with EmptyView, you can just return View for the happy path.
Empty return.
For function marked with ViewBuilder, you don't need to return anything
Return nil.
Optional conforms to SwiftUI View as long as its Wrapped conforms to View. So we can return nil from ViewBuilder
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.