Check if a BOOL is set (can't be done with ==nil)
Solution 1
You can't. A BOOL
is either YES
or NO
. There is no other state. The way around this would be to use an NSNumber
([NSNumber numberWithBool:YES];
), and then check to see if the NSNumber
itself is nil
. Or have a second BOOL
to indicate if you've altered the value of the first.
Solution 2
Annoyingly, Objective-C has no Boolean class. It certainly feels like it should and that trips a lot of people up. In collections and core data, all bools are stored as NSNumber instances.
It's really annoying having to convert back and forth all the time.
Solution 3
By default, a bool value is set to 0 in Objective-C, so you don't need to check if your bool value is nil anytime.
![Alon Amir](https://i.stack.imgur.com/hwJg5.jpg?s=256&g=1)
Comments
-
Alon Amir almost 2 years
how do i check if a BOOL is set in objective-c (iphone)?
i know that it can be done with an int or float this way: NSNumber *Num = [prefs floatForKey:@"key"]; for example