Bird
0
0

Given this code, what will be displayed?

medium📝 ui behavior Q4 of 15
iOS Swift - SwiftUI Layout
Given this code, what will be displayed?
struct ContentView: View {
  let animals = ["Cat", "Dog", "Bird"]
  var body: some View {
    List(animals, id: \.self) { animal in
      Text(animal)
    }
  }
}
AA scrollable list showing Cat, Dog, Bird each on its own row
BA single Text view showing all animals concatenated
CAn empty view with no content
DA compile error due to missing id
Step-by-Step Solution
Solution:
  1. Step 1: Analyze List with array and id

    The List uses animals array with id: \.self to uniquely identify each string.
  2. Step 2: Understand the closure

    Each animal string is shown as a Text view in its own row.
  3. Final Answer:

    A scrollable list showing Cat, Dog, Bird each on its own row -> Option A
  4. Quick Check:

    List with id and closure shows each item separately [OK]
Quick Trick: Use id: \.self for arrays of strings in List [OK]
Common Mistakes:
  • Thinking List concatenates all items into one Text
  • Assuming List shows nothing without explicit id
  • Confusing id usage causing compile errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes