Hi,
I am building a multiplatform app that uses core data.
I want to add some delete functionality.
When I created the project, apple provided some basic functionality which works for iOS, but not for mac.
For deletion on iOS, you would use the .onDelete(perform:) modifier. When I pass in that method that takes in a IndexSet, the modifier passes the indexSet automatically to the method.
This is the delete function:
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
print(error)
}
}
}
This way of deleting works great on iOS, but MacOS doesn’t have that modifier. It has the onDeleteCommand(perform:) modifier. That modifier triggers a function when the users selects an item, and goes to the menu bar, and selects edit > delete. But, as that modifer doesnt pass in a IndexSet automatically like the onDelete modifier, this doesn’t work.
Is there a way of of getting the indexSet into a (state) variable that will be updated when a user selects an item, which I then could pass into the method when calling the .onDeleteModifier ?