0Pricing
SwiftUI Academy · Lektion

Identifiable und stabile IDs

Machen Sie Modelle für korrektes Diffing Identifiable.

Identifiable und stabile IDs ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Identifiable und stabile IDs“ kostenlos?

Ja — der vollständige Text von „Identifiable und stabile IDs“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Identifiable und stabile IDs“?

Machen Sie Modelle für korrektes Diffing Identifiable. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Identifiable und stabile IDs“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Eine statische Liste erstellen
  2. Schleifen mit ForEach
  3. Identifiable und stabile IDs
  4. Abschnitte und Listenstile
← Zurück zu SwiftUI Academy