Objective-C中的YES / NO,TRUE / FALSE和true / false

确实是一个简单的问题;这些值之间是否有区别(BOOL和bool之间是否有区别)?一位同事提到他们在Objective-C中对不同的事物进行评估,但是当我查看各自.h文件中的typedef时,YES / TRUE / true都定义为,1而NO / FALSE / false都定义为0。真的有区别吗?



qq_花开花谢_0
浏览 1425回答 3
3回答

万千封印

如果您将BOOL变量用作布尔值,则没有实际区别。C根据布尔表达式的计算结果是否为0或0来处理它们。if(someVar ) { ... }if(!someVar) { ... }的意思与if(someVar!=0) { ... }if(someVar==0) { ... }这就是为什么您可以将任何原始类型或表达式作为布尔测试(包括指针)进行评估的原因。请注意,您应该做前者,而不是后者。请注意,如果您将钝值分配给所谓的变量并测试特定值,则有所不同BOOL,因此请始终将其用作布尔值,并仅从其#define值中分配它们。重要的是,永远不要使用字符比较来测试布尔值-这是冒险的,因为someVar可以为它分配一个非零的非YES值,但我认为更重要的是,它不能正确表达意图: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] 12013-02-19 20:30:37.061 BooleanTests[27433:a0f] 12013-02-19 20:30:37.072 BooleanTests[27433:a0f] 12013-02-19 20:30:37.073 BooleanTests[27433:a0f] 12013-02-19 20:30:37.073 BooleanTests[27433:a0f] 12013-02-19 20:30:37.074 BooleanTests[27433:a0f] 12013-02-19 20:30:37.074 BooleanTests[27433:a0f] 12013-02-19 20:30:37.075 BooleanTests[27433:a0f] 12013-02-19 20:30:37.075 BooleanTests[27433:a0f] 12013-02-19 20:30:37.076 BooleanTests[27433:a0f] 12013-02-19 20:30:37.077 BooleanTests[27433:a0f] 12013-02-19 20:30:37.077 BooleanTests[27433:a0f] 12013-02-19 20:30:37.078 BooleanTests[27433:a0f] 12013-02-19 20:30:37.078 BooleanTests[27433:a0f] 12013-02-19 20:30:37.079 BooleanTests[27433:a0f] 12013-02-19 20:30:37.079 BooleanTests[27433:a0f] 12013-02-19 20:30:37.080 BooleanTests[27433:a0f] 12013-02-19 20:30:37.080 BooleanTests[27433:a0f] 12013-02-19 20:30:37.081 BooleanTests[27433:a0f] 02013-02-19 20:30:37.081 BooleanTests[27433:a0f] 02013-02-19 20:30:37.082 BooleanTests[27433:a0f] 02013-02-19 20:30:37.091 BooleanTests[27433:a0f] 02013-02-19 20:30:37.092 BooleanTests[27433:a0f] 02013-02-19 20:30:37.093 BooleanTests[27433:a0f] 02013-02-19 20:30:37.093 BooleanTests[27433:a0f] 02013-02-19 20:30:37.094 BooleanTests[27433:a0f] 02013-02-19 20:30:37.094 BooleanTests[27433:a0f] 02013-02-19 20:30:37.095 BooleanTests[27433:a0f] 02013-02-19 20:30:37.095 BooleanTests[27433:a0f] 02013-02-19 20:30:37.096 BooleanTests[27433:a0f] 02013-02-19 20:30:37.096 BooleanTests[27433:a0f] 02013-02-19 20:30:37.097 BooleanTests[27433:a0f] 02013-02-19 20:30:37.098 BooleanTests[27433:a0f] 02013-02-19 20:30:37.101 BooleanTests[27433:a0f] 02013-02-19 20:30:37.102 BooleanTests[27433:a0f] 02013-02-19 20:30:37.102 BooleanTests[27433:a0f] 0
打开App,查看更多内容
随时随地看视频慕课网APP