Supprimer l'object dans datatables de base

Comment je peux supprimer un object que j'avais déjà ajouté avec ce code. C'est une section de favoris, au début, j'ajoute une écanvas grise qui ajoute un object venant d'un fetch. Puis il devient jaune et la méthode en arrière doit être jaune écanvas = supprime.

Mais je n'ai aucune idée de comment faire ça.

Merci d'avance

-(IBAction)inFavoris:(id)sender { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObject *favorisObj = [NSEntityDescription insertNewObjectForEntityForName:@"Favoris" inManagedObjectContext:context]; [favorisObj setValue:idTaxi forKey:@"idTaxi"]; [favorisObj setValue:nomTaxi forKey:@"nomTaxi"]; [favorisObj setValue:taxiCB forKey:@"cb"]; [favorisObj setValue:taxiAvion forKey:@"avion"]; [favorisObj setValue:taxiColis forKey:@"colis"]; [favorisObj setValue:taxiHandicape forKey:@"handicape"]; [favorisObj setValue:taxiHoraires forKey:@"horaire"]; [favorisObj setValue:lugagge forKey:@"lugagge"]; [favorisObj setValue:luxury forKey:@"luxury"]; [favorisObj setValue:languesParlees forKey:@"langues"]; [favorisObj setValue:taxiNote forKey:@"note"]; [favorisObj setValue:taxiPassengers forKey:@"passenger"]; [favorisObj setValue:taxiVote forKey:@"ecanvass"]; [favorisObj setValue:taxiTel forKey:@"tel"]; [self.view addSubview:favorisB]; } 

Mettre à jour

J'ai fait cette méthode .. Il fait le travail 🙂

 -(IBAction)outFavoris:(id)sender { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSSsortingng *testEntityId = idTaxi; NSManagedObjectContext *moc2 = [appDelegate managedObjectContext]; NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; fetch.entity = [NSEntityDescription entityForName:@"Favoris" inManagedObjectContext:moc2]; fetch.predicate = [NSPredicate predicateWithFormat:@"idTaxi == %@", testEntityId]; NSArray *array = [moc2 executeFetchRequest:fetch error:nil]; for (NSManagedObject *managedObject in array) { [moc2 deleteObject:managedObject]; } [self.view addSubview:favorisO]; } 

C'est assez simple 🙂

 [context deleteObject:favorisObj]; 

Et le mauvais object est parti.

Mettre à jour

Vous inverseriez juste avec quelque chose comme ceci si vous avez besoin d'un button pour supprimer l'object.

 -(IBAction)removeFavoris:(id)sender { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; [context deleteObject:favorisObj]; } 

N'oubliez pas de sauvegarder le context après avoir supprimé un NSManagedObject. Alors voici le code général;

 NSManagedObjectContext * context = [self managedObjectContext]; [context deleteObject:objectToDelete]; NSError * error = nil; if (![context save:&error]) { NSLog(@"Error ! %@", error); } 

Dans votre cas, il devrait avoir l'extrait après la boucle for.

 for (NSManagedObject *managedObject in array) { [moc2 deleteObject:managedObject]; } NSError * error = nil; if (![context save:&error]) { NSLog(@"Error ! %@", error); }