PruitIgoe
PruitIgoe

Reputation: 6384

iOS Casting to a BOOL

Are these two lines of code different or the same (Edit: Do they do the same thing, typecast to a boolean?):

BOOL isRegistered = (BOOL)[[NSUserDefaults standardUserDefaults] valueForKey:@"DEVICE_REGISTERED"];

BOOL isRegistered = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Is Registered"] boolValue];

In iOS8.1 the first one is definitely failing. The cast to a bool is not working, the cast is always returning as false. The value is being stored (for the second example) like so:

[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%i", registerSuccess] 
                                          forKey:@"Is Registered"];

registerSuccess is a value returned from a web service (JSON).

Upvotes: 0

Views: 1619

Answers (4)

vikingosegundo
vikingosegundo

Reputation: 52227

You cast a NSString object to a bool, that will never do anything useful. A cast does not change or convert something, it just silences the compiler.

Assuming that isRegistered is a NSNumber object, as it would be deserialised as such from the JSON response: Use a NSNumber

[[NSUserDefaults standardUserDefaults] setObject:registerSuccess forKey:@"isRegistered"];

and

BOOL isRegistered = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isRegistered"] boolValue];

or a Bool directly

[[NSUserDefaults standardUserDefaults] setBool:[registerSuccess boolValue]forKey:@"isRegistered"];

BOOL isRegistered = [[NSUserDefaults standardUserDefaults] boolForKey:@"isRegistered];

Upvotes: 6

ondermerol
ondermerol

Reputation: 536

I use below code to write and read a boolean value


// assume that "isDeviceRegisterred" is a boolean data type parameter that comes from the web service
[[NSUserDefaults standardUserDefaults] setBool:isDeviceRegisterred  forKey:@"DEVICE_REGISTERED"];

BOOL isRegistered = [[NSUserDefaults standardUserDefaults] boolForKey:@"DEVICE_REGISTERED"];

Upvotes: 1

glyuck
glyuck

Reputation: 3397

First of all, consider using NSUserDefaults boolForKey:.

Answering your question:

BOOL isRegistered = (BOOL)[[NSUserDefaults standardUserDefaults] valueForKey:@"DEVICE_REGISTERED"];

Here NSUserDefaults return NSNumber, and you are trying to cast pointer to NSNumber into BOOL. If your settings is not defined at all, valueForKey will return nil, it will be considered false. If any value is set for this settings, valueForKey will return pointer to NSNumber and when you will try to convert it to BOOL, your will get always True.

BOOL isRegistered = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isRegistered"] boolValue];

Here NSUserDefaults return NSNumber to you and you call boolValue on it, and it's working as you are expecting. If settings is not defined at all, valueForKey will return nil, calling boolValue on nil will return another nil, which is cast to false;

Upvotes: 2

Steve
Steve

Reputation: 1840

I would suggest saving the value as follows:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:registerSuccess] forKey:@"DEVICE_REGISTERED"];

Then read the value back as you do in the second line of the code you show, but with the key @"DEVICE_REGISTERED".

Upvotes: 1

Related Questions