<i id='txLEO'><tr id='txLEO'><dt id='txLEO'><q id='txLEO'><span id='txLEO'><b id='txLEO'><form id='txLEO'><ins id='txLEO'></ins><ul id='txLEO'></ul><sub id='txLEO'></sub></form><legend id='txLEO'></legend><bdo id='txLEO'><pre id='txLEO'><center id='txLEO'></center></pre></bdo></b><th id='txLEO'></th></span></q></dt></tr></i><div id='txLEO'><tfoot id='txLEO'></tfoot><dl id='txLEO'><fieldset id='txLEO'></fieldset></dl></div>
        <bdo id='txLEO'></bdo><ul id='txLEO'></ul>

        <tfoot id='txLEO'></tfoot>

        <small id='txLEO'></small><noframes id='txLEO'>

      1. <legend id='txLEO'><style id='txLEO'><dir id='txLEO'><q id='txLEO'></q></dir></style></legend>
      2. 在objective-c中YES/NO,TRUE/FALSE和true/false之间有区别吗?

        时间:2023-10-05
      3. <small id='owvYo'></small><noframes id='owvYo'>

          <tbody id='owvYo'></tbody>
        <tfoot id='owvYo'></tfoot>

        • <bdo id='owvYo'></bdo><ul id='owvYo'></ul>

                  <legend id='owvYo'><style id='owvYo'><dir id='owvYo'><q id='owvYo'></q></dir></style></legend>
                  <i id='owvYo'><tr id='owvYo'><dt id='owvYo'><q id='owvYo'><span id='owvYo'><b id='owvYo'><form id='owvYo'><ins id='owvYo'></ins><ul id='owvYo'></ul><sub id='owvYo'></sub></form><legend id='owvYo'></legend><bdo id='owvYo'><pre id='owvYo'><center id='owvYo'></center></pre></bdo></b><th id='owvYo'></th></span></q></dt></tr></i><div id='owvYo'><tfoot id='owvYo'></tfoot><dl id='owvYo'><fieldset id='owvYo'></fieldset></dl></div>
                  本文介绍了在objective-c中YES/NO,TRUE/FALSE和true/false之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  Simple question really; is there a difference between these values (and is there a difference between BOOL and bool)? A co-worker mentioned that they evaluate to different things in Objective-C, but when I looked at the typedefs in their respective .h files, YES/TRUE/true were all defined as 1 and NO/FALSE/false were all defined as 0. Is there really any difference?

                  推荐答案

                  没有实际区别提供你使用 BOOL 变量作为布尔值.C 会根据布尔表达式的计算结果是否为 0 来处理布尔表达式.所以:

                  There is no practical difference provided you use BOOL variables as booleans. C processes boolean expressions based on whether they evaluate to 0 or not 0. So:

                  if(someVar ) { ... }
                  if(!someVar) { ... }
                  

                  意思相同

                  if(someVar!=0) { ... }
                  if(someVar==0) { ... }
                  

                  这就是为什么您可以将任何原始类型或表达式评估为布尔测试(包括,例如指针).请注意,您应该做前者,而不是后者.

                  which is why you can evaluate any primitive type or expression as a boolean test (including, e.g. pointers). Note that you should do the former, not the latter.

                  请注意,如果您将钝值分配给所谓的 BOOL 变量并测试特定值,则存在差异,因此始终将它们用作布尔值并仅从它们的 #define 值中分配它们.

                  Note that there is a difference if you assign obtuse values to a so-called BOOL variable and test for specific values, so always use them as booleans and only assign them from their #define values.

                  重要的是,永远不要使用字符比较来测试布尔值——这不仅是有风险的,因为 someVar 可以被分配一个不是 YES 的非零值,但在我看来更重要的是,它失败了正确表达意图:

                  Importantly, never test booleans using a character comparison -- it's not only risky because someVar could be assigned a non-zero value which is not YES, but, in my opinion more importantly, it fails to express the intent correctly:

                  if(someVar==YES) { ... } // don't do this!
                  if(someVar==NO ) { ... } // don't do this either!
                  

                  换句话说,按照预期和记录使用的构造来使用,这样您就不会在 C 语言中受到伤害.

                  In other words, use constructs as they are intended and documented to be used and you'll spare yourself from a world of hurt in C.

                  这篇关于在objective-c中YES/NO,TRUE/FALSE和true/false之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Objective-C:BOOL vs bool 下一篇:iOS:在 NSUserDefaults 中使用布尔值

                  相关文章

                1. <tfoot id='5i3gi'></tfoot>
                  <legend id='5i3gi'><style id='5i3gi'><dir id='5i3gi'><q id='5i3gi'></q></dir></style></legend>

                    <bdo id='5i3gi'></bdo><ul id='5i3gi'></ul>

                    <small id='5i3gi'></small><noframes id='5i3gi'>

                      <i id='5i3gi'><tr id='5i3gi'><dt id='5i3gi'><q id='5i3gi'><span id='5i3gi'><b id='5i3gi'><form id='5i3gi'><ins id='5i3gi'></ins><ul id='5i3gi'></ul><sub id='5i3gi'></sub></form><legend id='5i3gi'></legend><bdo id='5i3gi'><pre id='5i3gi'><center id='5i3gi'></center></pre></bdo></b><th id='5i3gi'></th></span></q></dt></tr></i><div id='5i3gi'><tfoot id='5i3gi'></tfoot><dl id='5i3gi'><fieldset id='5i3gi'></fieldset></dl></div>