简单的问题; 这些值之间是否存在差异(BOOL和bool之间是否存在差异)?一位同事提到他们在Objective-C中评估不同的东西,但是当我查看各自的.h文件中的typedef时,YES/TRUE/true都被定义为,1
而NO/FALSE/false都被定义为0
.真的有什么区别吗?
我相信有是之间的差异bool
和BOOL
,看看这个网页为什么的解释:
http://iosdevelopertips.com/objective-c/of-bool-and-yes.html
因为BOOL
是一个unsigned char
而不是一个原始类型,所以类型的变量BOOL
可以包含除YES
和之外的值NO
.
考虑以下代码:
BOOL b = 42; if (b) { printf("b is not NO!\n"); } if (b != YES) { printf("b is not YES!\n"); }
输出是:
b不是NO!
b不是!
对于大多数人来说,这是一个不必要的问题,但如果你真的想要一个布尔值,最好使用一个bool
.我应该补充一点:iOS SDK通常BOOL
在其接口定义上使用,因此这是一个坚持的参数BOOL
.
如果您使用BOOL
变量作为布尔值,则没有实际差异.C根据它们是否计算为0来处理布尔表达式.所以:
if(someVar ) { ... } if(!someVar) { ... }
意思是一样的
if(someVar!=0) { ... } if(someVar==0) { ... }
这就是为什么你可以将任何原始类型或表达式评估为布尔测试(包括例如指针).请注意,你应该做前者,而不是后者.
请注意,如果将obtuse值分配给所谓的变量并测试特定值,则会有所不同BOOL
,因此请始终将它们用作布尔值,并仅从其#define
值中分配它们.
重要的是,永远不要使用字符比较来测试布尔值 - 这不仅有风险,因为someVar
可以分配一个非零的非零值,但在我看来更重要的是,它无法正确表达意图:
if(someVar==YES) { ... } // don't do this! if(someVar==NO ) { ... } // don't do this either!
换句话说,使用构造,因为它们的目的和记录使用,你将使自己免于C的伤害世界.
我对此做了详尽的测试.我的结果应该说明一切:
//These will all print "1" NSLog(@"%d", true == true); NSLog(@"%d", TRUE == true); NSLog(@"%d", YES == true); NSLog(@"%d", true == TRUE); NSLog(@"%d", TRUE == TRUE); NSLog(@"%d", YES == TRUE); NSLog(@"%d", true == YES); NSLog(@"%d", TRUE == YES); NSLog(@"%d", YES == YES); NSLog(@"%d", false == false); NSLog(@"%d", FALSE == false); NSLog(@"%d", NO == false); NSLog(@"%d", false == FALSE); NSLog(@"%d", FALSE == FALSE); NSLog(@"%d", NO == FALSE); NSLog(@"%d", false == NO); NSLog(@"%d", FALSE == NO); NSLog(@"%d", NO == NO); //These will all print "0" NSLog(@"%d", false == true); NSLog(@"%d", FALSE == true); NSLog(@"%d", NO == true); NSLog(@"%d", false == TRUE); NSLog(@"%d", FALSE == TRUE); NSLog(@"%d", NO == TRUE); NSLog(@"%d", false == YES); NSLog(@"%d", FALSE == YES); NSLog(@"%d", NO == YES); NSLog(@"%d", true == false); NSLog(@"%d", TRUE == false); NSLog(@"%d", YES == false); NSLog(@"%d", true == FALSE); NSLog(@"%d", TRUE == FALSE); NSLog(@"%d", YES == FALSE); NSLog(@"%d", true == NO); NSLog(@"%d", TRUE == NO); NSLog(@"%d", YES == NO);
输出是:
2013-02-19 20:30:37.061 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.061 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.072 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.073 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.073 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.074 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.074 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.075 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.075 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.076 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.077 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.077 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.078 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.078 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.079 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.079 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.080 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.080 BooleanTests[27433:a0f] 1 2013-02-19 20:30:37.081 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.081 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.082 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.091 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.092 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.093 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.093 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.094 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.094 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.095 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.095 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.096 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.096 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.097 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.098 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.101 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.102 BooleanTests[27433:a0f] 0 2013-02-19 20:30:37.102 BooleanTests[27433:a0f] 0
您可能想要阅读此问题的答案.总之,在Objective-C中(来自objc.h中的定义):
typedef signed char BOOL; // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" // even if -funsigned-char is used. #define OBJC_BOOL_DEFINED #define YES (BOOL)1 #define NO (BOOL)0
主要的(危险!)之间的差异true
,并YES
在JSON序列化.
例如,我们有JSON类型的服务器请求,需要在json sence中发送true/false:
NSDictionary *r1 = @{@"bool" : @(true)}; NSDictionary *r2 = @{@"bool" : @(YES)}; NSDictionary *r3 = @{@"bool" : @((BOOL)true)};
然后我们在发送之前将其转换为JSON字符串
NSData *data = [NSJSONSerialization dataWithJSONObject:requestParams options:0 error:nil]; NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
结果是
jsonString1 // {"bool":1} jsonString2 // {"bool":true} jsonString3 // {"bool":true}
由于API逻辑jsonString1
可能会导致错误.
因此,请注意Objective-C中的布尔值.
PS你可以使用
@{@"bool" : @"true"}; // in JSON {"bool":true}