following the Core Data tutorial (and Collection Views), I have a problem when I try to delete…
In this code, Swift compiler gives me this error: “Cannot convert value of type ‘[IndexPath]?’ to expected argument type ‘IndexPath’”
let indexPath = myCollectionView.indexPathsForSelectedItems
let bullet = fetchedRC.object(at: indexPath) <<<<<<< IN THIS POINT!
context.delete(bulletRaw)
appDelegate.saveContext()
I am following “Beginning Core Data” (PetPal App example), after complain the tutorial "Beginning Collection Views ". But the delete option in Core Data is not developed in tutorial “Beginning Core Data”.
myCollectionView.indexPathsForSelectedItems is returning an optional array of IndexPaths. That’s what [IndexPath]? means.
fetchedRC.object(at:) expects a single IndexPath, not an array.
So do something like
if let paths = myCollectionView.indexPathsForSelectedItems, paths.count > 0 {
let indexPath = paths[0] //get first path
let bullet = fetchedRC.object(at: indexPath)
// ...
}