The ViewBuilder Attribute

<p>Difficulty: Beginner |&nbsp;<strong>Easy</strong>&nbsp;| Normal | Challenging</p> <p>This article has been developed using Xcode 14.2, and Swift 5.7.2</p> <h2>Prerequisites:</h2> <p>You need to be able to code in Swift, perhaps using</p> <p><a href="https://medium.com/@stevenpcurtis.sc/coding-in-swift-playgrounds-1a5563efa089" rel="noopener">Playgrounds</a>&nbsp;and be able to use SwiftUI</p> <p>You might need to know something about&nbsp;<a href="https://medium.com/@stevenpcurtis/swifts-result-builders-abc0391f25b6" rel="noopener">result builders</a>&nbsp;in order to happily follow this article</p> <h2>Terminology</h2> <p>@resultBuilder: An attribute that transforms a sequence of statements into a single combined value</p> <p>View: In SwiftUI a view is a protocol that defines a piece of user interface and is a building block for creating UI in SwiftUI</p> <p>ViewBuilder: A SwiftUI attribute that enables the dynamic composition of views based on conditions without explicitly returning them</p> <h1>Why?</h1> <h2>Simplified syntax</h2> <p>Put simply you want to use `<code>@ViewBuilder</code>` to create child views for a SwiftUI view without having to use return.</p> <p>You will notice that something like this `<code>View</code>` avoids ugly use of return or commas in between the `<code>Text</code>` views:</p> <p><a href="https://stevenpcurtis.medium.com/the-viewbuilder-attribute-fe7c98c68e4e">Website</a></p>