0

I have a menu displaying files that are stored in a Core Data model. I'm able to add a new object to the model and display it on the menu. Now, I would like to delete one file from the menu when right clicking on it and choose delete, everything I tried didn't work so far:

- (IBAction)RemoveSelectedFile:(id)sender {
    if ([((NSMenuItem *)sender).menu isEqual:self.fileRecordContextMenu]) {

        // get the indices that have been clicked on
        NSIndexSet * indices = [self _indexesToProcessForContextMenuForTable:self.fileRecordsTable];
        ClassFileRecord * fileRecord = (self.document && self.document.fileRecordsController && self.document.fileRecordsController.arrangedObjects && indices && indices.count > 0) ? [self.document.fileRecordsController.arrangedObjects objectAtIndex:indices.firstIndex] : nil;

        if (fileRecord) {

            // Path to the file
            NSString * u = fileRecord.sourceFilePath;

            if (u) {
                // Delete Object

            }
        }
    }
}

UPDATE:

I found an easy solution using the NSArrayController:

 NSArray * selectedObjects = self.fileRecordsController.selectedObjects;
[self.fileRecordsController removeObjects:selectedObjects];
5
  • What did you try. "How do I delete an object from Core Data" has been asked many times. Commented May 10, 2017 at 13:52
  • As I said, I tried many things: meaning I've looked on the internet and tried them.. but without success Commented May 10, 2017 at 13:58
  • Possible duplicate of Delete object in Core Data Commented May 10, 2017 at 14:06
  • NSManagedObjectContext has a method for this purpose. Did you try it? If it didn't work, what specifically went wrong? Saying "didn't work" is not a useful question. Commented May 10, 2017 at 14:39
  • See my update, thanks! Commented May 10, 2017 at 14:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.