Challenge - 5 Problems
Dynamic List 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 to create rows.
✗ Incorrect
The List uses the array 'fruits' and creates one row per item, showing each fruit name separately.
❓ lifecycle
intermediate2:00remaining
How does SwiftUI update a List when data changes?
If you change the array that a SwiftUI List uses, how does the List update?
Attempts:
2 left
💡 Hint
SwiftUI watches data changes to update views.
✗ Incorrect
SwiftUI uses data bindings and state to detect changes and automatically refresh the UI, including Lists.
🧠 Conceptual
advanced2:00remaining
Why use 'id: \.self' in a SwiftUI List?
What is the purpose of specifying 'id: \.self' when creating a List in SwiftUI?
Attempts:
2 left
💡 Hint
Think about how SwiftUI tracks which rows changed.
✗ Incorrect
The 'id' parameter helps SwiftUI know which items are which, so it can update only changed rows efficiently.
🔧 Debug
advanced2:00remaining
What error occurs with this List code?
What error will this SwiftUI code produce when compiling?
iOS Swift
struct ContentView: View {
let numbers = [1, 2, 3]
var body: some View {
List(numbers, id: \.self) { number in
Text("Number: " + String(number))
}
}
}Attempts:
2 left
💡 Hint
Check if the data type is Identifiable or if 'id' is provided.
✗ Incorrect
List requires items to be Identifiable or have an 'id' parameter. Int is not Identifiable, so the compiler errors.
expert
3:00remaining
How to navigate to a detail view from a List row?
Which code snippet correctly navigates to a detail view when tapping a row in a SwiftUI List?
iOS Swift
struct ContentView: View {
let items = ["One", "Two", "Three"]
var body: some View {
NavigationView {
List(items, id: \.self) { item in
// Navigation code here
}
.navigationTitle("Items")
}
}
}
struct DetailView: View {
let item: String
var body: some View {
Text("Detail for \(item)")
}
}Attempts:
2 left
💡 Hint
Use the standard SwiftUI way to link views in a list.
✗ Incorrect
NavigationLink wraps the row content and specifies the destination view for navigation.