这真的让我很烦。在 Objective-C 中,我有一个Item
带有布尔属性的实体Deleted
。我希望能够将 的值设置Deleted
为 YES 或 1。
这是我的代码:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSString *itemID = [[fetchedObjects objectAtIndex:(int)[currentTable selectedRow]] valueForKey:@"ItemID"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ItemID = %@", itemID];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
Item *objectToDelete = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];
if (objectToDelete == nil) {
NSLog(@"ERROR");
}
[fetchRequest release];
[objectToDelete setValue:[NSNumber numberWithBool:YES] forKey:@"Deleted"];
[managedObjectContext save:&error];
需要注意的是,我能够成功更改不同的属性;例如:我将字符串 -DEL 添加到Item
的属性Code
和Name
. 当我查看数据表时,这些值的字符串会相应更新,但值Deleted
仍然为 0。