You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI无需模型遵循Equatable协议?其重绘机制与效率探讨

Why SwiftUI Relies on Identifiable Instead of Mandating Equatable?

Great question—this cuts to the core of how SwiftUI handles view identity and updates, which is a super common point of confusion for developers getting to grips with the framework. Let’s break this down step by step:

1. Identifiable is about view identity, not content equality

First, let’s clarify what Identifiable does. Its sole job is to give SwiftUI a stable way to track which view corresponds to which model in a collection (like a list or grid). When your models conform to Identifiable, SwiftUI uses the id property to match old and new views during its diffing process:

  • If an id exists in the new collection but not the old, SwiftUI adds a new view.
  • If an id exists in the old but not the new, SwiftUI removes that view.
  • If an id is present in both, SwiftUI knows it’s the same view and can update it in place (instead of tearing it down and rebuilding it).

Without Identifiable, SwiftUI falls back to comparing elements by their position in the collection—this leads to buggy behavior (like wrong animations when reordering items) and unnecessary view rebuilds.

2. SwiftUI doesn’t need Equatable because it uses other update triggers

So why not require Equatable to check if the model’s content changed? Because SwiftUI has built-in mechanisms to detect when updates are needed, regardless of Equatable:

  • For ObservableObject models: When you use @ObservedObject or @StateObject, SwiftUI listens to the model’s @Published properties. If any of those properties change, the model sends a notification, and SwiftUI updates only the views that depend on that changed property. The id tells SwiftUI which view to update, and the @Published changes tell it what to update.
  • For value-type models (structs): If your Identifiable model is a struct stored in @State or passed via @Binding, SwiftUI automatically compares the old and new values (even without Equatable conformance). If the values are different, it updates the view. If you do conform to Equatable, you can customize this comparison (e.g., ignore properties that don’t affect the UI) to make the check more efficient.

3. What happens when the same id has different content?

Let’s split this into two cases:

  • ObservableObject: If the id is the same but the model’s content changes (via a @Published property), SwiftUI gets a notification from the model and updates the corresponding view. It doesn’t need to check for equality because the publisher already told it something changed.
  • Value-type struct: SwiftUI will compare the old and new struct instances. If they’re different (even with the same id), it updates the view. If they’re the same, it skips the update.

4. Is this approach inefficient?

Short answer: No—SwiftUI is designed to be extremely efficient even without Equatable. Here’s why:

  • View updates are granular: SwiftUI only updates the parts of the view hierarchy that depend on the changed state, not the entire screen.
  • Views are lightweight structs: "Redrawing" a SwiftUI view doesn’t mean rebuilding the underlying UIKit/AppKit view—it just means recalculating the view’s description, which is fast.
  • The diffing algorithm is optimized: SwiftUI’s diffing logic (powered by Identifiable) ensures that only necessary views are added, removed, or updated.

That said, adding Equatable conformance to value-type models can give you an extra layer of optimization, especially if your model has properties that don’t affect the UI. By customizing the == operator to ignore those properties, you can prevent unnecessary updates.

Wrapping up

To recap:

  • Identifiable solves the problem of tracking view identity in collections.
  • Equatable solves the problem of checking if content has changed (for optimization).
  • SwiftUI doesn’t mandate Equatable because it has other, more direct ways to detect updates (publishers for reference types, value comparison for structs).
  • Even without Equatable, SwiftUI’s update mechanism remains efficient thanks to its granular diffing and lightweight view design.

内容的提问来源于stack exchange,提问作者Cheok Yan Cheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:03:10