Bird
0
0

What output will this SwiftUI code produce?

medium📝 Predict Output Q4 of 15
iOS Swift - Lists and Data Display
What output will this SwiftUI code produce?
let cities = ["Paris", "London", "Tokyo"]

List {
  ForEach(cities, id: \.self) { city in
    Text(city)
  }
}
AAn empty list because the id is incorrect.
BA single row showing all city names concatenated.
CA list displaying "Paris", "London", and "Tokyo" as separate rows.
DA runtime error due to missing Identifiable conformance.
Step-by-Step Solution
Solution:
  1. Step 1: Understand ForEach with array of strings

    Using ForEach(cities, id: \.self) iterates over each city string.
  2. Step 2: What does List display?

    Each city is displayed in its own Text view as a separate row.
  3. Final Answer:

    A list displaying "Paris", "London", and "Tokyo" as separate rows. -> Option C
  4. Quick Check:

    ForEach with id: \.self shows each string separately [OK]
Quick Trick: ForEach with id: \.self shows each string separately [OK]
Common Mistakes:
  • Assuming concatenation of strings
  • Confusing id requirement with Identifiable protocol

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes