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. NSManagedObject
already has methods closely related to this name (in particular -isDeleted
) which is potentially resulting in collisions with your custom attribute name. As the documentation for NSPropertyDescription
says:
Note that a property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject. For example, you cannot give a property the name "description". There are hundreds of methods on NSObject which may conflict with property names—and this list can grow without warning from frameworks or other libraries. You should avoid very general words (like "font”, and “color”) and words or phrases which overlap with Cocoa paradigms (such as “isEditing” and “objectSpecifier”).
Does your code work if you change the name of your property to something not likely to collide with any other property/method names?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With