Bird
0
0

What is wrong with this SwiftUI List code?

medium📝 Debug Q14 of 15
iOS Swift - SwiftUI Layout
What is wrong with this SwiftUI List code?
struct ContentView: View {
  let items = ["A", "B", "C"]
  var body: some View {
    List(items) { item in
      Text(item)
    }
  }
}
AMissing id parameter for simple array causes a compile error
BText view cannot display strings directly
CList requires items to be Int type
DClosure syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check List initializer requirements

    When using List with simple arrays like [String], you must provide an id parameter to identify each element uniquely.
  2. Step 2: Identify missing id parameter

    The code omits id: \.self, so it will cause a compile-time error.
  3. Final Answer:

    Missing id parameter for simple array causes a compile error -> Option A
  4. Quick Check:

    Simple arrays need id: \.self in List = D [OK]
Quick Trick: Always add id: \.self for simple arrays in List [OK]
Common Mistakes:
  • Assuming List infers id automatically
  • Thinking Text can't display strings
  • Believing List only works with Int arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes