Back to Swift Reference Ask BeeAI
📱 SwiftUIBeginneriOS 13+
List, ForEach & Identifiable
Render dynamic collections efficiently with stable identity, swipe actions and sections.
List builds a scrolling, cell-reusing list. ForEach generates views for each element and needs stable identity — conform your model to Identifiable.
Add swipe actions, onDelete and sections to get native iOS behavior for free.
list-and-foreach.swift
1struct Todo: Identifiable {
2 let id = UUID()
3 var title: String
4}
5
6struct TodoList: View {
7 @State private var todos = [Todo(title: "Learn SwiftUI"), Todo(title: "Ship app")]
8
9 var body: some View {
10 List {
11 Section("Today") {
12 ForEach(todos) { todo in
13 Text(todo.title)
14 .swipeActions {
15 Button("Delete", role: .destructive) {
16 todos.removeAll { $0.id == todo.id }
17 }
18 }
19 }
20 }
21 }
22 }
23}Common pitfalls
- Avoid id: \.self for values that can repeat — duplicate IDs cause glitches and crashes.
Still unclear? Ask BeeAI about List, ForEach & Identifiable.
Get a tailored explanation, or paste your own code for a Swift 6 review. Free members get daily questions — Pro is unlimited.
Related topics
Become a Founding Pro member: unlimited BeeAI, upcoming Swift paths and every new reference entry first.
See Pro Plans