SwiftUIMidMCQ

What is the `Identifiable` protocol and why does SwiftUI need it?

Test your knowledge:

Explanation & Code

Answer: Identifiable requires a type to have a unique id property. SwiftUI uses this in ForEach and List to track which items are which across state changes — enabling correct animations and avoiding UI glitches.

Code Example:

struct Article: Identifiable {
    let id: UUID
    let title: String
}

// SwiftUI can now track each Article by id
List(articles) { article in
    Text(article.title)
}

// Without Identifiable — must provide keypath manually
List(articles, id: \.title) { article in
    Text(article.title)
}
// ⚠️ Only safe if titles are unique — id should always be truly unique

Key Points:

  • id can be any Hashable type — UUID, Int, String
  • Using a non-unique id causes incorrect animations and potential crashes
  • UUID() is the safest default for new models

Rate your understanding:

Ready to practice more SwiftUI?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.