onDeleteCommand modifier

Has anyone had success getting the .onDeleteCommand() modifier to work with a List?

.Delete() seems to be working fine, and enables swipe-to-delete on any platform, and I figured the onDeleteCommand modifier was the key to enabling the Edit > Delete menu on macOS, but no matter where I put the modifier in my view the Edit > Delete command remains disabled.
Answered by Angelo in 633223022
In beta 6 of Xcode 12/Big Sur, I've been able to get this working on macOS simply by attaching this modifier to a List, like so:

Code Block swift
var body: some View {
List {
/* Your list-populating ForEach goes here */
}
.onDeleteCommand(perform: { print("Delete command received!") })
}


A pitfall to look out for: I use .deleteDisabled() conditionally on each NavigationLink in my List, and that's ignored by .onDeleteCommand(), so I return early in the action if my disable condition is met.

The .onDelete() modifier on the ForEach respects .deleteDisabled() when I two-finger swipe on the Mac's trackpad, though.
Accepted Answer
In beta 6 of Xcode 12/Big Sur, I've been able to get this working on macOS simply by attaching this modifier to a List, like so:

Code Block swift
var body: some View {
List {
/* Your list-populating ForEach goes here */
}
.onDeleteCommand(perform: { print("Delete command received!") })
}


A pitfall to look out for: I use .deleteDisabled() conditionally on each NavigationLink in my List, and that's ignored by .onDeleteCommand(), so I return early in the action if my disable condition is met.

The .onDelete() modifier on the ForEach respects .deleteDisabled() when I two-finger swipe on the Mac's trackpad, though.
onDeleteCommand modifier
 
 
Q