0
0
iOS Swiftmobile~20 mins

LazyVStack and LazyHStack in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lazy Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
        }
      }
    }
  }
}
AAn empty view because LazyVStack requires a fixed height.
BA vertical list of three items: Item 1 (green), Item 2 (blue), Item 3 (green), each 50 points tall.
CA vertical list of three items all colored blue.
DA horizontal list of three items with alternating colors.
Attempts:
2 left
💡 Hint
LazyVStack arranges views vertically and loads them lazily inside a ScrollView.
ui_behavior
intermediate
2: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)
        }
      }
    }
  }
}
AThe app will load all 1000 items at once, which may cause slow performance or memory issues.
BThe app will load items lazily as the user scrolls, improving performance.
CThe app will crash because VStack cannot be inside ScrollView.
DThe app will display only the first 10 items and ignore the rest.
Attempts:
2 left
💡 Hint
VStack creates all its child views immediately, unlike LazyVStack.
lifecycle
advanced
2: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?
ALazyHStack creates child views only when they are about to appear on screen, improving performance for large data sets.
BLazyHStack creates all child views immediately when the view appears.
CLazyHStack never creates any child views until the user taps on them.
DLazyHStack creates child views randomly, not in order.
Attempts:
2 left
💡 Hint
Think about how lazy loading helps with performance in scrollable horizontal stacks.
📝 Syntax
advanced
2: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)")
    }
  }
}
AError: LazyVStack requires a fixed frame height.
BError: Missing id parameter in ForEach causing a compiler error.
CError: Text views inside LazyVStack must have unique tags.
DNo error; the code compiles and runs correctly.
Attempts:
2 left
💡 Hint
ForEach with a range automatically provides an id.
🔧 Debug
expert
2: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)
    }
  }
}
AText views inside LazyHStack cannot have background colors.
BLazyHStack requires a fixed height to enable scrolling.
CThe ScrollView defaults to vertical scrolling; horizontal axis must be specified explicitly.
DForEach must have an explicit id for horizontal scrolling to work.
Attempts:
2 left
💡 Hint
Check the ScrollView initializer for axis direction.