Choose OrderedSet When Your UI Needs Order And Membership
A photo picker exports images in the order the user selected them. An Array preserves that order but permits duplicates. A Set removes duplicates but does not represent the selection sequence. Keeping both in sync becomes another small source of bugs.
Swift Collections’ OrderedSet fits this particular contract: unique members in a user-defined order.
Store stable IDs, not mutable rows
The package’s OrderedSet documentation describes ordered uniqueness, membership checks, and indexed traversal. For a selection model, make the members the stable IDs of selected items.
import OrderedCollections
struct Selection {
private(set) var ids: OrderedSet<Int> = []
mutating func select(_ id: Int) { ids.append(id) }
mutating func deselect(_ id: Int) { ids.remove(id) }
mutating func reconcile(available: Set<Int>) {
ids = OrderedSet(ids.filter { available.contains($0) })
}
var exportOrder: [Int] { Array(ids) }
}
Selecting 8, then 3, then 8 again produces [8, 3]. Selecting an already-selected member does not mean “move it to the end.” That distinction is useful when a repeated tap or duplicate event arrives.
Deselecting 8 and selecting it again produces [3, 8]. In this model, a new selection joins the end. If your product wants to restore the old position, that needs an explicit history policy; a collection cannot infer it.
Keep titles, thumbnails, and mutable metadata in the existing item store. A change to a title should not change whether the item is selected. Making a whole mutable row Hashable can accidentally tie selection identity to presentation fields.
Reconcile against authoritative availability
The reconciliation method removes unavailable IDs while preserving the relative order of survivors. It does not sort by ID, by server order, or by the order of a newly fetched page.
Only call it when available represents the complete authoritative set for this selection scope. If the user has selected photos across several pages, reconciling against the latest page alone would erase perfectly valid selections from earlier pages.
When availability is unresolved, keep the selection and represent that loading state separately. This is the same distinction that matters in navigation restoration: not loaded does not mean deleted.
Order is part of equality
Two ordered sets with the same members in different orders are different ordered selections. That is useful for an export queue, where moving one image changes the result even if membership stays constant.
If a particular comparison needs membership only, compare an unordered view or separate Sets deliberately. Do not accidentally suppress an export-order update by reducing both selections to Sets before deciding whether anything changed.
Indices also describe current positions, not permanent identities. Removing a member can shift later indices. SwiftUI rows should use stable item IDs rather than treating an index captured before removal as the identity of a selected image.
Know the tradeoff before replacing every Set
OrderedSet keeps array-like ordering in addition to membership information. Insertion or removal in the middle can require shifting elements and updating positions. It is not a promise of constant-time arbitrary deletion just because membership lookup is efficient.
For a small UI selection, that tradeoff is often reasonable. For a large queue with frequent front removal, compare the actual operations against a queue-oriented collection instead. For a handful of static choices, an Array with a clear uniqueness rule may already be sufficient.
The Swift Collections package provides the OrderedCollections product. The companion package pins version 1.2.0 to make this established API example reproducible; that pin is not a recommendation to downgrade an app already using a newer compatible release. Keep your app’s dependency policy and resolved versions intact.
September 2026 review: the companion executable resolved Swift Collections 1.2.0 and passed uniqueness, insertion order, deselection/reselection, availability reconciliation, and order-sensitive equality checks with Swift 6.4.
The useful simplification is one collection expressing one product promise. Selection is both a set of identities and a sequence the user created.