How to delete Core Data from Collection View?

Hello from Barcelona!

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()

Do you know what happens?

Thank you very much!

best regards.
Miquel

@mcarreras What tutorial are you following exactly?

Hi shogunkaramazov!

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”.

Thanks and best regards,
Miquel

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)
  // ...
}

Hi Sgerrard,

Thanks!!

I’ll try your suggested code… I’ll give you feedback.

best regards,
Miquel

This topic was automatically closed after 166 days. New replies are no longer allowed.