0

我正在尝试删除特定属性

tipdescription | tip number | tipindex    

go to school   |  001       |   21

come home      |  004       |   54

lock home      |  008       |   86

我将如何删除整列“锁定家”?我试过下面的代码

app = [[ UIApplication sharedApplication] delegate]; 

NSFetchRequest *req = [[NSFetchRequest alloc]init];

[req setEntity:[NSEntityDescription entityForName:@"Tips" inManagedObjectContext:app.managedObjectContext]];
NSString *deletestatement =[[NSString alloc] initWithFormat:@"lock home"];

[req setPredicate:[NSPredicate predicateWithFormat:@"tipdescription LIKE  %@", deletestatement]];
NSError *error;
Tips *std = [[app.managedObjectContext executeFetchRequest:req error:&error] lastObject];

[app.managedObjectContext deleteObject:std];

运行它后,我收到以下错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-deleteObject:需要非零参数

4

1 回答 1

0

为什么不使用如下谓词:

[req setPredicate:[NSPredicate predicateWithFormat:@"tipdescription ==  %@", deletestatement]];

一种更干净的方法是针对小费编号进行谓词(我想它是一个标识符并且是一个NSString

[req setPredicate:[NSPredicate predicateWithFormat:@"tipnumber ==  %@", @"001"]];

此外,在执行 fetch 请求时进行一些条件控制:

NSManagedObjectContext* myContext = app.managedObjectContext;

NSArray* result = [myContext executeFetchRequest:req error:&error];
if([result count] > 0) {

    Tips *std = (Tips*)[result objectAtIndex:0];
    [myContext deleteObject:std];
} else {

    NSLog(@"No result");
}
于 2012-07-09T09:39:33.003 回答