CoreData entity with Boolean attribute being saved as an NSNumber object

Niazipan picture Niazipan · May 28, 2013 · Viewed 8.2k times · Source

Wondering if anyone else had come across this, or if there's a reason and I'm doing something wrong.

I have an app with CoreData. In the schema I have a 'content' entity with an 'unlocked' attribute which is set to Boolean.

However when I save out the Obj C class for the entity though Xcode, unlocked appears within content.h as:

@property (nonatomic, retain) NSNumber * unlocked;

If I change it to Boolean in content.h, I get an ARC compiling error. However if I leave it as an NSNumber object when I try and fetch it, it's coming back inconsistently (as in if I have an NSLog printing it, it comes back as a different value each time I run).

I can figure out a fairly obvious work-around, setting unlocked as an NSString to 'yes' or 'no' and compare that at the relevant point, but I wanted to know if anyone knew why this was happening or if there is way to keep it as a Boolean.

Thanks in advance.

Answer

ceekay picture ceekay · May 28, 2013

CoreData stores objects, which BOOL is not.

[NSNumber numberWithBool:YES]

Is the way to set the attribute and you can use it by reading mybool = [content.unlocked boolValue];