0
0
iOS Swiftmobile~20 mins

List with ForEach in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
SwiftUI List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What will this SwiftUI List display?
Consider this SwiftUI code snippet. What will the List show when the app runs?
iOS Swift
struct ContentView: View {
  let fruits = ["Apple", "Banana", "Cherry"]
  var body: some View {
    List {
      ForEach(fruits, id: \.self) { fruit in
        Text(fruit)
      }
    }
  }
}
AA list showing numbers 0, 1, 2 instead of fruit names.
BA list showing Apple, Banana, and Cherry as separate rows.
CA list showing only Apple because ForEach stops after first item.
DThe code will not compile due to missing id parameter.
Attempts:
2 left
πŸ’‘ Hint
Think about what ForEach does with an array and the id parameter.
πŸ“ Syntax
intermediate
2:00remaining
Identify the syntax error in this ForEach usage
Which option correctly fixes the syntax error in this SwiftUI code?
iOS Swift
struct ContentView: View {
  let numbers = [1, 2, 3]
  var body: some View {
    List {
      ForEach(numbers) { number in
        Text("\(number)")
      }
    }
  }
}
ARemove the closure parameter: ForEach(numbers) { Text("\(number)") }
BChange numbers to a Set: ForEach(Set(numbers)) { number in ... }
CAdd id: \.self to ForEach: ForEach(numbers, id: \.self) { number in ... }
DReplace List with VStack: VStack { ForEach(numbers) { number in ... } }
Attempts:
2 left
πŸ’‘ Hint
ForEach needs a way to identify each item uniquely when using simple arrays.
❓ lifecycle
advanced
2:00remaining
What happens if the data array changes in a ForEach List?
If the array used in ForEach changes (items added or removed), what does SwiftUI do to the List?
ASwiftUI updates the List to reflect the new array contents automatically.
BThe List does not update until the app restarts.
CThe List duplicates old and new items causing visual glitches.
DSwiftUI crashes because ForEach cannot handle dynamic data.
Attempts:
2 left
πŸ’‘ Hint
Think about how SwiftUI reacts to state changes.
πŸ”§ Debug
advanced
2:00remaining
Why does this ForEach cause a runtime crash?
This code crashes at runtime. What is the cause?
iOS Swift
struct ContentView: View {
  let items = ["A", "B", "A"]
  var body: some View {
    List {
      ForEach(items, id: \.self) { item in
        Text(item)
      }
    }
  }
}
AThe array has duplicate items used as id, causing duplicate id error.
BForEach cannot use strings as id, only integers.
CList requires a section header, missing here.
DText views must have unique tags, missing here.
Attempts:
2 left
πŸ’‘ Hint
Check the uniqueness of the id values.
🧠 Conceptual
expert
2:00remaining
Why use ForEach inside a List instead of just List with data?
Which reason best explains why developers use ForEach inside a List in SwiftUI?
AUsing ForEach inside List is required to enable scrolling.
BList cannot display arrays directly without ForEach.
CForEach improves app performance by caching views automatically.
DForEach allows more control over each row’s view and supports complex layouts inside List.
Attempts:
2 left
πŸ’‘ Hint
Think about flexibility in building UI rows.