Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData entity with Boolean attribute being saved as an NSNumber object

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.

like image 260
Niazipan Avatar asked May 28 '13 17:05

Niazipan


1 Answers

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];

like image 84
ceekay Avatar answered Oct 06 '22 19:10

ceekay