Challenge - 5 Problems
List View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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)
}
}
}Attempts:
2 left
💡 Hint
Think about how List uses the array and the id parameter to create rows.
✗ Incorrect
The List uses the fruits array and creates a row for each fruit string. The id: \.self means each string is unique and used as an identifier.
📝 Syntax
intermediate2: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)")
}
}
}Attempts:
2 left
💡 Hint
List needs a way to identify each item uniquely when the data is not identifiable by default.
✗ Incorrect
The List initializer requires an id parameter when the data elements do not conform to Identifiable. Adding id: \.self fixes the syntax error.
❓ lifecycle
advanced2: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")
}
}
}
}Attempts:
2 left
💡 Hint
Think about how @State works in SwiftUI and how views update.
✗ Incorrect
Using @State allows the view to update when fruits changes. Adding Cherry triggers the view to refresh and show the new item.
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?
}
}
}
}Attempts:
2 left
💡 Hint
NavigationLink wraps the row content to enable navigation.
✗ Incorrect
NavigationLink creates a tappable row that navigates to the destination view when tapped.
🔧 Debug
expert2: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)
}
}
}
}Attempts:
2 left
💡 Hint
Think about how ForEach with a range behaves when the array changes size.
✗ Incorrect
Using a range for ForEach means indices are fixed at creation. After deletion, indices may be invalid causing crash. Using ForEach over the array elements directly avoids this.