views:

458

answers:

2

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

+7  A: 

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.

Dave DeLong
Actually, a `BOOL` may not be `YES` or `NO`. The only safe way to work with `BOOL` is to check wither it is either `NO` or `!NO`. `BOOL` is a `typedef` for `signed char`.
dreamlax
@dreamlax True, you could set a BOOL to 1 or 2, but to me that seems like a perversion of the purpose of the typedef.
Dave DeLong
dreamlax
+1  A: 

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.

TechZen
What would you gain having a Boolean class that you don't have with NSNumber? You'd still have to convert back and forth with scalar BOOLs for storage in things like NSArray and Core Data.
Brad Larson