1. Definition

@ViewBuilder is a result builder that lets you write multiple views inside a closure and combine them into a single some View.


2. Why it exists

SwiftUI requires a single return type (some View), but we often write multiple views.

@ViewBuilder transforms:

VStack {
    Text("A")
    Text("B")
}

into a single combined view.


3. Where it’s used

VStack
HStack
ZStack
Group
custom view builders

4. What it allows

VStack {
    Text("A")

    if condition {
        Text("B")
    } else {
        Text("C")
    }
}

5. What it does NOT allow

for item in items { }   ❌
while condition { }     ❌

Use:

ForEach(...)            βœ