我正在使用 UICollectionView,通过这种方法,我可以删除我添加到 CoreData 的所有对象:
- (IBAction)btnDelete:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"Delete all favorites?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
nil;
} else if (buttonIndex == 1){
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *execQuery =[[NSFetchRequest alloc] init];
NSEntityDescription *descOggetto = [NSEntityDescription entityForName:@"HexCode" inManagedObjectContext:context];
[execQuery setEntity:descOggetto];
NSError *error = nil;
_arr = [context executeFetchRequest:execQuery error:&error];
for (NSManagedObject *ogg in _arr){
[context deleteObject:ogg];
[_favoriteCollectionView reloadData];
}
NSError *saveError = nil;
[context save:&saveError];
// NSLog(@"Delete");
}
}
如何删除单个对象而不是全部?