Understanding View Identity
Control how SwiftUI diffs and reuses views.
Understanding View Identity is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
Identity Drives SwiftUI
SwiftUI tracks each view by its identity to decide whether to update an existing view or create a new one.
Getting identity right is the single biggest lever for correctness and performance.
Two Kinds of Identity
SwiftUI uses two forms: structural identity (a view’s position in the view tree) and explicit identity (an id you assign).
Structural Identity
By default, a view’s identity comes from where it sits in the hierarchy. The if branches below create two structurally distinct views.
var body: some View {
if isOn {
Text("On") // one identity
} else {
Text("Off") // a different identity
}
}Why Branches Lose State
Because each branch is a different structural identity, switching between them destroys the first view and creates the second. Any @State inside is reset.
if isExpanded {
DetailView() // new instance each time the branch flips
} else {
SummaryView()
}Explicit Identity with id()
The .id(_:) modifier assigns explicit identity. Changing the id tells SwiftUI "this is a different view", forcing a fresh instance and state reset.
CounterView()
.id(selectedTab) // new identity per tabIdentity in ForEach
ForEach needs stable identity per element. Use a key path to a stable id, never the array index for mutable collections.
ForEach(items, id: \.id) { item in
RowView(item: item)
}The Index Identity Trap
Using id: \.self on indices or values that change reorders identities when the data mutates, causing wrong animations and lost state.
// Risky if items reorder or values repeat:
// ForEach(0..<items.count, id: \.self) { ... }Identity and Animation
Animations interpolate between states of the same identity. If identity changes, SwiftUI does an insert/remove transition instead of a smooth animation.
withAnimation {
// same identity -> property animates
// changed id -> transition (fade/slide)
}Conditional Modifiers Change Identity
Applying a modifier inside an if can change structural identity, resetting state. Prefer a single view with the modifier value computed conditionally.
// Better: keep one identity
Text("Hi")
.opacity(isHidden ? 0 : 1)
// Worse: branching creates two identitiesAnyView Erases Identity Info
Wrapping views in AnyView hides their type from SwiftUI, weakening its ability to diff efficiently. Use concrete types or @ViewBuilder instead where possible.
// Prefer @ViewBuilder over returning AnyView
@ViewBuilder
func content() -> some View {
if flag { A() } else { B() }
}Stable Identity = Stable State
The rule of thumb: keep identity stable when you want state and smooth animation preserved; change identity deliberately when you truly want a fresh start.
Quick Check: View Identity
Test your understanding of view identity.
Recap: Understanding View Identity
SwiftUI identifies views structurally (tree position) and explicitly (.id). Stable identity preserves @State and enables smooth property animation; changing identity resets state and triggers transitions.
Give ForEach stable ids, avoid index identity for mutable data, minimize branching that flips identity, and avoid AnyView where a concrete type works.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Understanding View Identity” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Understanding View Identity”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Understanding View Identity”?
Control how SwiftUI diffs and reuses views. You practise Swift 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 Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Understanding View Identity” 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 Swift Academy lesson?
Yes. Every Swift 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
- Understanding View Identity
- Minimizing Body Re-evaluation
- Equatable Views and @State Scope
- Profiling with the SwiftUI Instrument