From Xcode 12, both switch and if let are supported in the ViewBuilder!
You typically use as a parameter attribute for child view-producingclosure parameters, allowing those closures to provide multiple child views. 👉 SwiftUI ⟩ ⟩
As for ViewBuilders, it is mainly used to create custom container views, which can also become a reusable view component.
👉
A good rule of thumb for me has to be to use a modifier first, and only use a builder when the code patterns really pulled strongly for that syntax.
👉 - NetSplit.com
You typically use ViewBuilder as a parameter attribute for child view-producing closure parameters, allowing those closures to provide multiple child views.
通常 @ViewBuilder closure 具有 @escaping 的特性,因為一般這個 closure 會在 .init(content:) 中傳進來,然後在 var body 中用到,如果沒有事先設為 @escaping 的話,那麼這個 closure 在 .init 中傳進來後,會隨著 .init 結束而消失,因此也沒辦法事後在 var body 中用到,所以會產生 compiler 錯誤。
問:那麼 @escaping closure 會存放在那裏呢 🤔❓
答:通常會存放在函數外部的變數或陣列中。
// ⭐️ Step 3: Implementing `View` protocol requirement
struct MyContainerView<Content: View>: View {
let content: Content
var body: some View {
// use `content` somewhere in your code
content
// ... (other customizations)
}
}
// ⭐️ Step 4: Use `MyContainerView` just like a `VStack` or `HStack`
struct ContentView: View {
var body: some View {
MyContainerView {
// content here ...
}
}
}