在objective-c中,YES / NO,TRUE / FALSE和true / false是否有区别?
简单的问题, 这些值之间有差异(和BOOL和布尔之间有区别)? 一位同事提到他们在Objective-C中对不同的东西进行评估,但是当我在各自的.h文件中查看typedef时,YES / TRUE / true被定义为1而NO / FALSE / false被定义为0 。 有没有什么区别? 
  如果您使用BOOLvariables作为布尔值,则没有实际区别。  C处理布尔expression式的基础是否评估为0或不是0.所以: 
 if(someVar ) { ... } if(!someVar) { ... } 
的意思是一样的
 if(someVar!=0) { ... } if(someVar==0) { ... } 
这就是为什么你可以评估任何原始types或expression式作为布尔testing(包括,例如指针)。 请注意,你应该做前者,而不是后者。
 请注意,如果将钝值分配给所谓的BOOLvariables并testing特定值, 则存在差异,因此始终将它们用作布尔值,并仅从它们的#define值中指定它们。 
 重要的是,不要使用字符比较来testing布尔值 – 这不仅是有风险的,因为someVar可以被赋值为非零值,但在我看来,更重要的是,它不能正确地expression意图: 
 if(someVar==YES) { ... } // don't do this! if(someVar==NO ) { ... } // don't do this either! 
换句话说,按照他们的意图使用结构并logging下来,你就可以免于受到C的伤害。
 我相信bool和BOOL 是有区别的,请查看这个网页来解释为什么: 
  http://iosdevelopertips.com/objective-c/of-bool-and-yes.html 
 因为BOOL是一个unsigned char而不是原始types,所以BOOLtypes的variables可以包含除YES和NO以外的值。 
考虑这个代码:
 BOOL b = 42; if (b) { printf("b is not NO!\n"); } if (b != YES) { printf("b is not YES!\n"); } 
输出是:
b不是不!
b不是的!
 对于大多数人来说,这是一个不必要的担心,但如果你真的想要一个布尔值,最好使用bool 。 我应该补充一点:iOS SDK通常在其接口定义上使用BOOL ,所以这是一个坚持BOOL的参数。 
我对此做了详尽的testing。 我的结果应该说明一切:
 //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 
 在JSON序列化中, true和YES之间的主要(危险!)区别。 
例如,我们有JSONtypes的服务器请求,需要在json sence中发送true / false:
 NSDictionary *r1 = @{@"bool" : @(true)}; NSDictionary *r2 = @{@"bool" : @(YES)}; NSDictionary *r3 = @{@"bool" : @((BOOL)true)}; 
然后我们在发送之前将它转换成JSONstring
 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"}; // {"bool":true} 
我认为他们在许多情况下加上“是/否”是更明确的。 例如:
 [button setHidden:YES]; 
听起来比
 [button setHidden:TRUE]; 
有一个微妙的错误,没有人在这里提到,我想我会包括…更多的逻辑错误比任何东西:
 int i = 2; if(i); //true if(i==YES); // false if((!!i)==YES); //true 
 所以这里的问题就是(YES==1) ,在C中比较不是一个布尔值,而是一个基于值的比较。 
 因为YES只是一个#define (而不是语言固有的东西),它必须是一些价值,而1是最有意义的。 
不,YES / NO是指TRUE / FALSE(1/0)的不同方式