Lists show changing information that can grow or shrink. They help apps display many items without fixed size.
Why lists present dynamic content in iOS Swift
struct ContentView: View {
let items: [String]
var body: some View {
List(items, id: \.self) { item in
Text(item)
}
}
}The List view shows rows for each item in a collection.
The id: \.self tells SwiftUI how to identify each item uniquely.
List(["Apple", "Banana", "Cherry"], id: \.self) { fruit in Text(fruit) }
List([] as [String], id: \.self) { item in Text(item) }
List(["Only one item"], id: \.self) { item in Text(item) }
This app shows a list of fruits. When you tap the button, it adds "Orange" to the list. The list updates automatically to show the new fruit.
import SwiftUI struct ContentView: View { @State private var fruits = ["Apple", "Banana", "Cherry"] var body: some View { VStack { List(fruits, id: \.self) { fruit in Text(fruit) } Button("Add Fruit") { fruits.append("Orange") } .padding() } } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Lists update automatically when the data changes if you use @State or other reactive properties.
Using unique IDs helps SwiftUI track which rows changed for smooth updates.
Common mistake: forgetting to provide an ID causes errors or wrong updates.
Lists show dynamic content that can change size and items.
Use lists when you want to display many items that update over time.
Provide unique IDs so the list updates correctly and smoothly.
Practice
Solution
Step 1: Understand the purpose of lists
Lists are designed to display multiple items, especially when the number of items can change.Step 2: Compare static vs dynamic content
Static content does not change, but dynamic content updates or changes size, which lists handle well.Final Answer:
Because lists can show many items that can change over time -> Option BQuick Check:
Lists = dynamic content display [OK]
- Thinking lists only show fixed content
- Confusing lists with buttons or static views
- Assuming lists create views automatically without code
items?Solution
Step 1: Identify correct List initializer
The List initializer requires an array and a unique id key path like \.self for simple types.Step 2: Check syntax correctness
List(items, id: \.self) { item in Text(item) } uses correct syntax: List(items, id: \.self) { item in Text(item) } which works for arrays of strings.Final Answer:
List(items, id: \.self) { item in Text(item) } -> Option AQuick Check:
Correct List syntax = List(items, id: \.self) { item in Text(item) } [OK]
- Omitting id parameter for dynamic lists
- Passing the whole array as Text content
- Using invalid id like the array itself
let fruits = ["Apple", "Banana", "Cherry"]
List(fruits, id: \.self) { fruit in
Text(fruit)
}Solution
Step 1: Understand the List content
The List uses the array fruits with id: \.self, so each fruit string is a unique row.Step 2: Predict UI output
The List will render three rows with Text views showing "Apple", "Banana", and "Cherry" respectively.Final Answer:
A list showing Apple, Banana, Cherry as text rows -> Option DQuick Check:
List with array and id shows each item as row [OK]
- Expecting concatenated text instead of separate rows
- Forgetting to add id causing runtime error
- Thinking list will be empty without explicit rows
let numbers = [1, 2, 3]
List(numbers) { number in
Text("Number: \(number)")
}Solution
Step 1: Identify the cause of error
List requires a unique id for each item when using an array of simple types like Int.Step 2: Fix by adding id parameter
Adding id: \.self tells SwiftUI how to identify each number uniquely.Final Answer:
Missing id parameter causes error; fix by adding id: \.self -> Option CQuick Check:
List needs id for simple arrays [OK]
- Ignoring missing id error
- Changing data type unnecessarily
- Confusing List with ForEach usage
Solution
Step 1: Understand importance of unique IDs in lists
Unique IDs help SwiftUI identify which items changed, added, or removed for smooth UI updates.Step 2: Choose correct List usage
Using List with user.id as id ensures each user row updates correctly even if order or content changes.Final Answer:
Use List with unique id for each user, like user.id, to track changes -> Option AQuick Check:
Unique id in List = smooth dynamic updates [OK]
- Omitting id causing UI glitches
- Using duplicate ids causing wrong updates
- Using static views instead of dynamic lists
Ready to prove it?
Pass the test and this pattern moves unknown
