I am using the new SwiftUI Table()
structure available for macOS.
This structure takes a selection of multiple rows as a Set<Object.ID>
and can be stored in a state property wrapper like so...
@State private var selectedPeople = Set<Person.ID>()
(per the example in the Apple documentation)
Table()
doesn't take any other type of identifier (that I am aware of), so I am constrained to use the ObjectIdentifier
unique identifier.
I am using Table()
to present a list of NSManagedObject
s.
These NSManagedObject
s are sourced from a Core Data Entity relationship property, in this case via a @FetchRequest
.
I want to be able to add new instances of the entity and also delete existing instances of the entity. I have worked out how to add a new instance - this is relatively easy.
What I am struggling to work out is how to delete an NSManagedObject
from the Table()
by using the Object.ID
that is used to track row selection.
I have attempted this...
@State private var tableSelection = Set<Action.ID>()
...
for actionID in tableSelection {
let context = event.managedObjectContext
let actionToDelete = context?.object(with: actionID)
print(actionToDelete)
context?.delete(actionToDelete)
}
but of course I have a type mismatch here and the compiler complains...
Cannot convert value of type 'Action.ID' (aka 'ObjectIdentifier') to expected argument type 'NSManagedObjectID'
So how I do grab a reference to the selected NSManagedObject
from the set of Action.ID
?