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).


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;

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


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

of a Bool directly

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

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

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".

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

Need Your Help

Redefine 'pause' behaviour in c++ console app in windows

c++ windows asynchronous io

I have a windows c++ console app and I want to redefine behaviour of program when I pressing 'pause' key durning runtime. How I can do this?

Android - Intent Filter?

android android-intent intentfilter

I am trying to register my Activity so that it can be used by the Activity chooser/picker allowing a user to choose whether or not to select my application/Activity to complete what they are trying...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.