First I convert BOOL value to NSNumber in order to put it into NSUserDefaults. Later I would like to retrieve the BOOL value from the NSUserDefaults, but obviously I get NSNumber instead of BOOL. My questions are?
Currently I have:
if (someNSNumberValue == [NSNumber numberWithBool:NO]) { do something }
any better way to to the comparison?
Thanks!
To get the bool value from a NSNumber use -(BOOL)boolValue : BOOL b = [num boolValue];
Overview. NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .
You currently compare two pointers. Use NSNumber
s methods instead to actually compare the two:
if([someNSNumberValue isEqualToNumber:[NSNumber numberWithBool:NO]]) { // ... }
To get the bool value from a NSNumber
use -(BOOL)boolValue
:
BOOL b = [num boolValue];
With that the comparison would be easier to read for me this way:
if([num boolValue] == NO) { // ... }
Swift 4:
let newBoolValue = nsNumberValue.boolValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With