Challenge - 5 Problems
Lazy Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the visible output of this SwiftUI code?
Consider this SwiftUI view using LazyVStack inside a ScrollView. What will the user see when the app runs?
iOS Swift
struct ContentView: View {
var body: some View {
ScrollView {
LazyVStack {
ForEach(1...3, id: \.self) { i in
Text("Item \(i)")
.frame(height: 50)
.background(i % 2 == 0 ? Color.blue : Color.green)
}
}
}
}
}Attempts:
2 left
💡 Hint
LazyVStack arranges views vertically and loads them lazily inside a ScrollView.
✗ Incorrect
LazyVStack creates a vertical stack that loads views only when needed. The ForEach creates three Text views with alternating background colors based on the item number.
❓ ui_behavior
intermediate2:00remaining
What happens if you replace LazyVStack with VStack in this code?
Given the same code as before but replacing LazyVStack with VStack inside ScrollView, what is the main difference in app behavior?
iOS Swift
struct ContentView: View {
var body: some View {
ScrollView {
VStack {
ForEach(1...1000, id: \.self) { i in
Text("Item \(i)")
.frame(height: 50)
}
}
}
}
}Attempts:
2 left
💡 Hint
VStack creates all its child views immediately, unlike LazyVStack.
✗ Incorrect
VStack renders all its children at once, so 1000 Text views are created immediately, which can slow down or crash the app. LazyVStack avoids this by creating views only when needed.
❓ lifecycle
advanced2:00remaining
Which statement about LazyHStack's view loading is true?
In SwiftUI, how does LazyHStack manage the creation of its child views compared to HStack?
Attempts:
2 left
💡 Hint
Think about how lazy loading helps with performance in scrollable horizontal stacks.
✗ Incorrect
LazyHStack delays creating child views until they are needed for display, unlike HStack which creates all children immediately. This lazy loading improves performance with many items.
📝 Syntax
advanced2:00remaining
What error does this LazyVStack code produce?
Identify the error in this SwiftUI code snippet:
iOS Swift
ScrollView {
LazyVStack {
ForEach(0..<5) { i in
Text("Item \(i)")
}
}
}Attempts:
2 left
💡 Hint
ForEach with a range automatically provides an id.
✗ Incorrect
ForEach over a range like 0..<5 automatically uses the integer as the id, so no explicit id is needed. LazyVStack does not require fixed height or tags on Text views.
🔧 Debug
expert2:00remaining
Why does this LazyHStack not scroll horizontally?
This SwiftUI code uses LazyHStack inside a ScrollView but the items do not scroll horizontally. What is the cause?
iOS Swift
ScrollView {
LazyHStack {
ForEach(0..<10) { i in
Text("Item \(i)")
.frame(width: 100, height: 50)
.background(Color.orange)
}
}
}Attempts:
2 left
💡 Hint
Check the ScrollView initializer for axis direction.
✗ Incorrect
By default, ScrollView scrolls vertically. To scroll horizontally, you must specify ScrollView(.horizontal). Without this, the LazyHStack content will not scroll horizontally.