Predict the output
The user taps Change. How does the label update?
struct Greeting: View {
@State private var name = "World"
var body: some View {
Text("Hello, \(name)")
Button("Change") { name = "Swift" }
}
}Hint 1 of 1
What is the source of truth here?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B SwiftUI re-runs body, diffs the result, and updates only the Text
Correct. State changes trigger a new body, and only what differs is updated.
Why not the others?
- A You must call a refresh method after changing name. SwiftUI has no manual refresh; changing state is enough.
- C The whole screen is torn down and rebuilt. View structs are recreated cheaply, but the underlying rendered views are updated in place.
- D Nothing changes until the view appears again. State changes update the UI immediately.