From my understanding of Core Data, all that is necessary for primitive accessors to work is the @dynamic directive for the property name (as well as declaring primitive accessors for that property within the entity implementation).
For some reason, when using the generated primitive accessor the setState: method is not modifying the state property:
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveState];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveState:[NSNumber numberWithInt:value]];
[self didChangeValueForKey:@"state"];
}
while using the key-value-coding version does modify the state property
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveValueForKey:@"state"];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveValue:[NSNumber numberWithInt:value] forKey:@"state"];
[self didChangeValueForKey:@"state"];
}
in both cases, I primitive accessors are declared as follows (and as per Apple’s example and code generation):
@interface Post (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveState;
- (void)setPrimitiveState:(NSNumber *)value;
@end
I’m a bit at a loss to why this would be. Any help would be greatly appreciated!
After tremendous amounts of head-scratching, debugging, fiddling and guess-and-check, I’ve finally figured out what the problem is: Core Data primitive accessors AREN’T dynamically generated if you define those attributes as instance variables. I had defined them for debugging purposes (as GBD cannot see the values of properties without defined ivars, it seems), and this prevented primitive accessors from being generated correctly. This is something that Apple should really document in some form. As it’s very difficult to discover on one’s own. I hope this helps others who’ve been having the same issue!