0
0
iOS Swiftmobile~20 mins

List view basics in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
List View 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(fruits, id: \.self) { fruit in
      Text(fruit)
    }
  }
}
AA list showing Apple, Banana, and Cherry as separate rows
BA list showing numbers 0, 1, 2 instead of fruit names
CAn empty list with no rows
DA single row showing all fruits concatenated as "AppleBananaCherry"
Attempts:
2 left
💡 Hint
Think about how List uses the array and the id parameter to create rows.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this List code
Which option correctly fixes the syntax error in this SwiftUI List code?
iOS Swift
struct ContentView: View {
  let numbers = [1, 2, 3]
  var body: some View {
    List(numbers) { number in
      Text("Number: \(number)")
    }
  }
}
AChange List to VStack: VStack(numbers) { number in ... }
BAdd id: \.self to List: List(numbers, id: \.self) { number in ... }
CRemove the closure: List(numbers)
DReplace Text with Button: List(numbers) { number in Button("(number)") {} }
Attempts:
2 left
💡 Hint
List needs a way to identify each item uniquely when the data is not identifiable by default.
lifecycle
advanced
2:00remaining
What happens when the data array changes in a SwiftUI List?
Given this code, what will happen if the fruits array changes after the view appears?
iOS Swift
struct ContentView: View {
  @State var fruits = ["Apple", "Banana"]
  var body: some View {
    VStack {
      List(fruits, id: \.self) { fruit in
        Text(fruit)
      }
      Button("Add Cherry") {
        fruits.append("Cherry")
      }
    }
  }
}
AThe app crashes because fruits is modified inside the view
BThe List stays the same and does not show Cherry
CThe List updates to show Apple, Banana, and Cherry after tapping the button
DThe button does nothing because fruits is a constant
Attempts:
2 left
💡 Hint
Think about how @State works in SwiftUI and how views update.
navigation
advanced
2:00remaining
How to navigate to a detail view from a List row?
Which code snippet correctly navigates to a DetailView when a list row is tapped?
iOS Swift
struct ContentView: View {
  let items = ["One", "Two"]
  var body: some View {
    NavigationView {
      List(items, id: \.self) { item in
        // What goes here?
      }
    }
  }
}
AListRow(item: item).navigate(to: DetailView(item: item))
BButton(action: { DetailView(item: item) }) { Text(item) }
CText(item).onTapGesture { NavigationLink(destination: DetailView(item: item)) }
DNavigationLink(destination: DetailView(item: item)) { Text(item) }
Attempts:
2 left
💡 Hint
NavigationLink wraps the row content to enable navigation.
🔧 Debug
expert
2:00remaining
Why does this List crash with 'Index out of range'?
This code crashes when tapping the Delete button. Why?
iOS Swift
struct ContentView: View {
  @State var fruits = ["Apple", "Banana", "Cherry"]
  var body: some View {
    List {
      ForEach(0..<fruits.count, id: \.self) { index in
        Text(fruits[index])
      }
      .onDelete { indices in
        fruits.remove(atOffsets: indices)
      }
    }
  }
}
AUsing a range 0..<fruits.count in ForEach causes index mismatch after deletion
Bfruits array is immutable and cannot be modified
ConDelete is not supported inside List
DThe id: \.self is missing in ForEach
Attempts:
2 left
💡 Hint
Think about how ForEach with a range behaves when the array changes size.