Reputation: 1037
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.
Upvotes: 9
Views: 8445
Reputation: 3541
BOOLs are stored like NSNumbers
in Core Data (if you look at your sqlite tables, you'll see they're stored as integers. So, you convert the BOOL
to NSNumber
before storing and convert the NSNumber
to BOOL
(or just us it as-is 0/1) when retrieving.
I've not seen any inconsistency, however -- if the stored NSNumber
is zero, it is equivalent to NO
and if nonzero, it is YES
.
Upvotes: 2
Reputation: 1157
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];
Upvotes: 12