Bird
0
0

Identify the error in this SwiftUI list code and how to fix it:

medium📝 Debug Q14 of 15
iOS Swift - Lists and Data Display
Identify the error in this SwiftUI list code and how to fix it:
let numbers = [1, 2, 3]

List(numbers) { number in
  Text("Number: \(number)")
}
AArray must be strings; convert numbers to strings first
BText interpolation is wrong; fix by removing \(number)
CMissing id parameter causes error; fix by adding id: \.self
DList cannot use closures; replace with ForEach
Step-by-Step Solution
Solution:
  1. Step 1: Identify the cause of error

    List requires a unique id for each item when using an array of simple types like Int.
  2. Step 2: Fix by adding id parameter

    Adding id: \.self tells SwiftUI how to identify each number uniquely.
  3. Final Answer:

    Missing id parameter causes error; fix by adding id: \.self -> Option C
  4. Quick Check:

    List needs id for simple arrays [OK]
Quick Trick: Always add id: \.self for lists with simple arrays [OK]
Common Mistakes:
  • Ignoring missing id error
  • Changing data type unnecessarily
  • Confusing List with ForEach usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes