In SwiftUI, how do List selection, EditButton and the editMode environment value work together so iPhone users can multi-select inbox messages?
answer
- a Set binding on the List
- selection type matches the row id
- checkmark circles appear in edit mode
- an optional Binding<EditMode>
basics
~20 sBind List(selection:) to a Set of the rows' ID type; on an iPhone, tap multi-selection works only in edit mode, which EditButton toggles through the editMode environment value that your own views can read or own.
solid answer
~40 s`List(selection: $selection)` with a `Set<Message.ID>` makes a multi-select list; a single optional ID makes a single-select one. The set's element type must match the rows' `Identifiable.ID`, because that is the value each row contributes. On a device without a keyboard and pointer, people can multi-select only while **edit mode** is active, when SwiftUI shows a circle with a checkmark beside each row; `EditButton` toggles the environment's `editMode` and reads Done while active. Single selection works outside edit mode from iOS 16. To show a batch Archive button only while editing, own an `@State EditMode` and inject it with `.environment(\.editMode, $editMode)`, so the button, the list and your code share it. `selectionDisabled(_:)` (iOS 17) keeps pinned rows out.
code
swift · 31 linesimport SwiftUI
struct Message: Identifiable { let id = UUID(); var subject: String; var isPinned = false }
struct InboxView: View {
@State private var messages: [Message] = []
@State private var selection = Set<Message.ID>()
@State private var editMode = EditMode.inactive
var body: some View {
NavigationStack {
List(selection: $selection) {
ForEach(messages) { message in
Text(message.subject)
.selectionDisabled(message.isPinned)
}
}
.toolbar {
EditButton()
if editMode.isEditing {
Button("Archive \(selection.count)") {
messages.removeAll { selection.contains($0.id) }
selection.removeAll()
}
.disabled(selection.isEmpty)
}
}
.environment(\.editMode, $editMode)
}
}
}go deeper
Recall that List(selection:) takes a binding, a Set means multi-select, and EditButton switches edit mode on and off with an Edit and Done title.
Explain the edit-mode rule for iPhone multi-select, why the selection type must match the row ids, and how editMode is an optional Binding<EditMode> in the environment.
Own EditMode in state when toolbar logic depends on it, prune the selection after batch actions, and use selectionDisabled for rows that must stay out.
Decide where batch actions live and how selection survives sync and deletion, so multi-select screens behave the same across the app.
## Selection is a binding on the List A SwiftUI `List` becomes selectable when you hand it a **binding**: - `List(selection: $selected)` where `selected` is `Message.ID?` creates a **single-selection** list. - The same initializer with a `Set<Message.ID>` creates a **multiple-selection** list. Each row contributes a value to that binding, and for rows produced by `ForEach` over `Identifiable` data that value is the element's `id`. Apple's documentation says the bound type must match the list data's `Identifiable.ID` type. A mismatch still compiles, because the binding only has to be `Hashable`, but the rows do not register as selected: a `Set<String>` of subjects cannot hold the `UUID` each row offers. Rows written by hand can declare their value with `tag(_:)`. ## Where edit mode comes in On iPhone, selection and edit mode are linked: 1. **Single selection** works with a plain tap on all platforms from iOS 16. Apple notes that in iOS 15 and earlier, lists supported selection only in edit mode, even for single selection. 2. **Multiple selection by tapping** requires edit mode on devices without an attached keyboard and mouse or trackpad. In edit mode the list shows a circle beside each row, with a checkmark for selected rows. 3. On iPad or Mac with a keyboard and pointer, people can multi-select **without** edit mode. Edit mode is an environment value, **`editMode`**, of type `Binding<EditMode>?`. `EditMode` has the cases `active`, `inactive` and `transient`, and an `isEditing` property. **`EditButton`** toggles that value for the content in its container, and its title changes from Edit to Done while active. The same value also makes a `ForEach` with `onDelete` or `onMove` show delete and move controls. ## Owning edit mode yourself A common requirement is a toolbar button such as "Archive 3" that appears only while editing. The view that builds the `List` needs to know the mode, so the cleanest approach is to own it: ```swift @State private var editMode = EditMode.inactive List(selection: $selection) { … } .toolbar { EditButton() if editMode.isEditing { Button("Archive \(selection.count)") { … } } } .environment(\.editMode, $editMode) ``` Now `EditButton` toggles **your** state, the list reads it, and your code can also leave edit mode after a batch action by setting `editMode = .inactive`. Child views read it with `@Environment(\.editMode) private var editMode` and test `editMode?.wrappedValue.isEditing == true`; the value is optional, so read it through `?.` rather than force-unwrapping it. ## The flow, step by step 1. The screen starts with `editMode` inactive and an empty `selection` set. 2. The person taps **Edit**. `EditButton` sets the environment's mode to `active`, the button now reads **Done**, and the list shows a selection circle beside each row. 3. Each tap toggles that row's `id` in your set, and the circle shows a checkmark for selected rows. 4. Your toolbar reads `selection.count` and offers the batch action. 5. The action changes the model, clears the set, and may set `editMode = .inactive` to leave edit mode in code, exactly as tapping **Done** would. Because every piece reads the same binding, nothing has to be synchronised by hand: the list, the button and your code agree on whether the screen is editing. ## Keeping the selection honest | Situation | What to do | |---|---| | A row must never be selected (a pinned message) | `selectionDisabled(_:)` on the row, from iOS 17 | | A batch action removes messages | clear or prune `selection`, or it keeps IDs that no longer exist | | Selection should drive a detail view | use an optional single-selection binding instead of a set | | The list is in a sheet without a navigation bar | place the `EditButton` where people can reach it, or set the mode in code | Some practical points: - The selection is **your state**. SwiftUI updates the set, but deleting rows does not remove their IDs from it. - Read `selection.count` for button titles and `.disabled(selection.isEmpty)` to block empty batch actions. - Each row view that reads the selection set depends on it; keep row views small so that cost stays low, a topic the lesson on redraw cost covers in depth. ## Common misunderstandings - Expecting a `Set` binding alone to enable tap multi-select on iPhone. - Treating `@Environment(\.editMode)` as a `Bool` or a non-optional value. - Binding the selection to whole model values or to a different key than the rows' `id`. - Assuming the iOS 15 rule (selection only in edit mode) still applies to single selection.
- How does single selection on an iPhone differ from multiple selection with a Set binding?From iOS 16, binding an optional ID lets people select one row with a plain tap outside edit mode, which suits a list that drives a detail view. A `Set` binding on a device without keyboard and pointer only allows tap multi-select once edit mode is on. In iOS 15 and earlier, even single selection needed edit mode.
- After archiving the selected messages, why might the Archive button still say Archive 2?The selection set is your state, and removing messages from the array does not remove their IDs from it. Clear or prune the set in the same action, for example `selection.removeAll()`, and set `editMode = .inactive` if the flow should leave edit mode.
- Binding a Set to List selection enables tap multi-select on iPhone by itself
- The editMode environment value is a plain Bool
- Any Hashable selection type works whatever the rows' id type is
- Single selection on iOS 17 still requires edit mode
- Deleting rows automatically removes their IDs from the selection set