I’m simplifying this for my question, but, as an example, let’s say I have a model with an entity named Employee and a Boolean attribute named vacationing which is non-optional but has a default value of NO set.
However, I’m seeing “vacationing is a required value” when attempting to save an update to an Employee entity. The code looks like this:
- (void)reinstateEmployee:(Employee*)employee context:(NSManagedObjectContext*)context {
employee.vacationing = NO;
NSError *error;
if (![context save:error]) {
NSLog(@"Error saving: %@", error.localizedDescription);
}
}
The problem here is that
NOis really just an alias for0which also representsnilandNULL.Since it’s perfectly valid to assign
nilto anNSNumberproperty, the compiler does not complain and instead of settingvacationingto false, it unsets it, which is not valid when the attribute is required.This is more obvious by replacing
NOwithYES, which will produce a compiler warning.To fix the issue, replace
NOwith@NOor[NSNumber numberWithBool:NO]to instead assign anNSNumberinstance.