Consider this SwiftUI view using a result builder to create a VStack. What text will be shown on the screen?
import SwiftUI struct ContentView: View { var body: some View { VStack { Text("Hello") Text("World") } } } // Assume this is run in a SwiftUI preview or app
Think about what VStack does and how result builders let you list views inside it.
VStack arranges views vertically. The result builder lets you list multiple views inside the closure, so Text("Hello") and Text("World") appear stacked vertically.
Which statement best describes how SwiftUI uses result builders?
Think about how SwiftUI lets you list views inside containers like VStack.
Result builders let you write multiple child views inside a closure, and SwiftUI combines them into a single container view like VStack or HStack.
Examine this SwiftUI code snippet. Why does it fail to compile?
import SwiftUI struct ContentView: View { var body: some View { VStack { if true { Text("Visible") } else { } Text("Always here") } } }
Think about how result builders handle conditional branches inside view containers.
SwiftUI's result builder requires all branches of an if-else to produce the same type. Without explicit typing or using Group, the compiler cannot infer the combined type, causing an error.
Choose the code snippet that correctly uses a result builder to create a view with two Text elements.
Remember how Swift separates statements inside closures.
In Swift closures, statements must be separated by semicolons or newlines. Option A correctly separates the two Text views with a semicolon.
Given this SwiftUI code, how many Text views will be created and displayed?
import SwiftUI struct ContentView: View { var body: some View { VStack { ForEach(0..<3) { i in if i % 2 == 0 { Text("Even: \(i)") } else { Text("Odd: \(i)") } } } } }
Count how many times the loop runs and what views it creates each time.
The ForEach runs 3 times (0,1,2). Each iteration creates one Text view, so total 3 Text views are created.