iPhone: Save boolean into Core Data
I have set up one of my core data attributes as a Boolean. Now, I need to set it, but XCode keeps telling me that it may not respond to setUseGPS.
[ride setUseGPS: useGPS.on];
What is the method for setting a boolean in core data? All my other attributes are set this way, and they work great. So, not sure why a boolean does not work to be set this way?
Solution 1:
Core Data "does not have" a Boolean type (it does, but it is an NSNumber).
So to set the equivalent of useGPS = YES.
[entity setUseGPS:[NSNumber numberWithBool:YES]];
And the other way around:
BOOL isGPSOn = [[entity useGPS] boolValue];
Update: As pointed out by SKG, With literals in Objetive-C you can now do it in a simpler way:
[entity setUseGPS:@YES];
BOOL isGPSOn = entity.useGPS.boolValue;
Solution 2:
As an alternative approach to the accepted answer, you can simply change the typing from an NSNumber* to a BOOL in the managed object interface definition, such as:
@property (nonatomic) BOOL useGPS; // Notice that the 'retain' is also removed as we're now dealing with a scalar rather than an NSObject
Various alternative approaches are discussed here, but Chris Hanson's response was most illuminating for me, especially:
If you have a numeric attribute (including a boolean attribute) that's required, you can just type it as a scalar instead, and Core Data will do the right thing:
@property (nonatomic) BOOL isDone;
Even if the attribute is optional, that'll still work - it'll just conflate "not present" with "false."
and for a more aligned Cocoa implementation:
One other thing you might want to do is name the property "done" and just specify the getter as "isDone." That's the usual Cocoa naming convention:
@property (nonatomic, getter=isDone) BOOL done;
Then you can write "if (item.done) { ... }" or "item.done = NO;" and the compiler will still generate -isDone for accesses of the property.
Thanks Chris, and hope that this helps someone.