iPhone: Save boolean into Core Data

Nic Hubbard picture Nic Hubbard · May 30, 2010 · Viewed 35.2k times · Source

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?

Answer

RickiG picture RickiG · May 30, 2010

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;