Identifiable & Stable IDs
Make models Identifiable for correct diffing.
Identifiable & Stable IDs is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Diffing Problem
When data changes, SwiftUI must know which rows moved, stayed, or vanished. Stable identity makes that diffing correct. 🧩
Why id: .self Is Fragile
Using id: .self breaks when two items are equal or a value changes, because the identity shifts unexpectedly.
Meet Identifiable
The Identifiable protocol asks a type for just one thing: a stable, unique id property that never changes for that item.
protocol Identifiable {
var id: ID { get }
}Conforming a Model
Add an id property and conform to Identifiable, and your struct is ready for clean, reliable lists.
struct Task: Identifiable {
let id = UUID()
var title: String
}UUID for Fresh IDs
A UUID generates a guaranteed-unique value, perfect when items have no natural identifier of their own.
let id = UUID()ForEach Without id:
Once a type is Identifiable, ForEach finds the id by itself, so you can drop the id parameter entirely.
ForEach(tasks) { task in
Text(task.title)
}Stable Means Persistent
A good id never changes for the same logical item, even when its title or other fields are edited later on.
Natural Keys
If your data already has a unique field, like a database id, use it instead of generating a new one.
struct User: Identifiable {
let id: Int
var name: String
}Why It Matters for Animation
Correct identity lets SwiftUI animate moves and deletes smoothly instead of redrawing everything.
Avoid Index as ID
Using array indexes as ids is risky: inserting or deleting shifts every index and confuses diffing.
Identity Is About Sameness
Two items with the same id are treated as the same row; different ids mean different rows. Choose ids carefully.
Quick Check
Pick the most reliable approach to row identity.
Recap
You learned Identifiable: a stable id per item lets SwiftUI diff and animate lists correctly, far safer than id: .self. 🎉
Frequently asked questions
Is the “Identifiable & Stable IDs” lesson free?
Yes — the full text of “Identifiable & Stable IDs” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Identifiable & Stable IDs”?
Make models Identifiable for correct diffing. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Identifiable & Stable IDs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Static List
- Looping with ForEach
- Identifiable & Stable IDs
- Sections & List Styles