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"开发者_JAVA技巧]; for example
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.
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.
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.
You can use something like this instead...
@import Foundation;
@interface CSBool : NSObject
+ (CSBool *)construct:(BOOL)value;
@property BOOL value;
@end
#import "CSBool.h"
@implementation CSBool
+ (CSBool *)construct:(BOOL)value {
CSBool *this = [self new];
this.value = value;
return this;
}
@end
精彩评论