Challenge - 5 Problems
ViewBuilder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this SwiftUI custom container?
Consider this SwiftUI custom container using @ViewBuilder. What will be displayed when the ContentView is rendered?
iOS Swift
struct CustomStack<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack {
Text("Header")
content()
Text("Footer")
}
}
}
struct ContentView: View {
var body: some View {
CustomStack {
Text("Hello")
Text("World")
}
}
}Attempts:
2 left
💡 Hint
Remember that the content closure is inserted inside the VStack between Header and Footer.
✗ Incorrect
The CustomStack uses a VStack with Text("Header"), then the content closure, then Text("Footer"). The content closure includes two Text views: "Hello" and "World". So the final vertical stack shows Header, Hello, World, Footer in order.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a SwiftUI custom container with @ViewBuilder?
You want to create a custom container that accepts multiple child views using @ViewBuilder. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Look for the correct generic constraint and @escaping closure syntax.
✗ Incorrect
Option A correctly uses a generic Content constrained to View, a @ViewBuilder closure marked @escaping, and calls content() inside the VStack. Option A incorrectly uses Content directly without closure. Option A uses 'View' protocol type directly which is not allowed. Option A misses @escaping on the closure parameter.
❓ lifecycle
advanced2:00remaining
How does @ViewBuilder affect the lifecycle of child views in a custom container?
In SwiftUI, when you use @ViewBuilder in a custom container, how does it influence the creation and update of child views?
Attempts:
2 left
💡 Hint
Think about how SwiftUI composes multiple views into one view tree.
✗ Incorrect
@ViewBuilder lets you write multiple child views inside a closure, which SwiftUI combines into a single View type. SwiftUI then manages their lifecycle efficiently, updating only what changes.
advanced
2:00remaining
What happens if you use @ViewBuilder with conditional views inside a custom container?
Given a custom container using @ViewBuilder, what is the effect of including an if-else condition inside the content closure?
iOS Swift
struct ConditionalContainer<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack { content() }
}
}
struct ContentView: View {
@State private var showText = true
var body: some View {
ConditionalContainer {
if showText {
Text("Visible")
} else {
Text("Hidden")
}
}
}
}Attempts:
2 left
💡 Hint
Remember @ViewBuilder supports conditional view building.
✗ Incorrect
@ViewBuilder supports if-else conditions to build different views dynamically. SwiftUI updates the view when the state changes, showing the correct text.
🔧 Debug
expert2:00remaining
Why does this custom container cause a compile error?
Examine this SwiftUI custom container code. Why does it fail to compile?
iOS Swift
struct BrokenContainer<Content: View>: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
VStack {
content
}
}
}
struct ContentView: View {
var body: some View {
BrokenContainer {
Text("Hello")
Text("World")
}
}
}Attempts:
2 left
💡 Hint
Think about what type the closure returns and what Content expects.
✗ Incorrect
The closure returns multiple views combined by @ViewBuilder as a TupleView, but Content is a single View type. Assigning multiple views to a single Content variable causes a type mismatch and compile error.