Bird
0
0

You want to display a list of user names with unique IDs. Which code correctly uses List with a custom struct User that conforms to Identifiable?

hard📝 Conceptual Q8 of 15
iOS Swift - SwiftUI Layout
You want to display a list of user names with unique IDs. Which code correctly uses List with a custom struct User that conforms to Identifiable?
AList(users, id: name) { user in Text(user.name) }
BList { ForEach(users) { Text(user.name) } }
CList(users) { Text(user.name) }
DList(users) { user in Text(user.name) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Identifiable protocol usage

    If User conforms to Identifiable, List can use users array directly without id parameter.
  2. Step 2: Check options

    List(users) { user in Text(user.name) } correctly uses List(users) with closure. List(users, id: name) { user in Text(user.name) } has syntax error (invalid id syntax). List(users) { Text(user.name) } has syntax error (missing closure parameter). List { ForEach(users) { Text(user.name) } } has syntax error (missing ForEach closure parameter).
  3. Final Answer:

    List(users) { user in Text(user.name) } -> Option D
  4. Quick Check:

    Identifiable array can be passed directly to List [OK]
Quick Trick: Use List(array) directly if elements are Identifiable [OK]
Common Mistakes:
  • Adding id parameter unnecessarily for Identifiable
  • Omitting closure parameter name
  • Wrapping List inside ForEach

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes