This is really annoying me. In Objective-C, I have an Item entity with a boolean attribute Deleted. I would like to be able to set the value of Deleted to YES or 1.
This is my code:
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];
Something to note is that I am able to successfully change different attributes; for example: I add the string -DEL to the end of the Item‘s attributes Code and Name. When I view a table of data, the strings for those values are updated correspondingly, however the value for Deleted continues to be 0.
I would highly highly highly recommend using a different name for this attribute.
NSManagedObjectalready has methods closely related to this name (in particular-isDeleted) which is potentially resulting in collisions with your custom attribute name. As the documentation forNSPropertyDescriptionsays:Does your code work if you change the name of your property to something not likely to collide with any other property/method names?