You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implementing UITableViewDataSource methods in TableViewDataSource #23
I wanted to implement swipe-to-delete in one of my UITableViewControllers. This VC displays a table of objects that inherit from ManagedObject and implement ManagedObjectType. To do so, the VC's dataSource needs to implement the tableView(tableView: commitEditingStyle: forRowAtIndexPath:) method, and handle the .Delete case. To delete I simply do the following:
/// ManagedObject - protocol extension that implements defaults
extension ManagedObject {
public func delete() {
managedObjectContext?.performChanges({
self.managedObjectContext?.deleteObject(self)
})
}
}
Because the TableViewDataSource does not have access to the NSManagedObjectContext, but the ManagedObject does, I am required to cast down to ManagedObject, as the TableViewDataSource's where clause does not actually require ManagedObject.
Is there a better way to handle this? Obviously, I would implement a guard in the cast, but I was wondering if there were a better way to implement deleting an object in this way? Without access to the moc, and without access to the tableView's delegate it seems I have to require some type constraints on the Object to be able to delete.
Activity