Objective-C TRUE/FALSE与true/false [英] Objective-C TRUE/FALSE vs true/false

查看:141
本文介绍了Objective-C TRUE/FALSE与true/false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我们有不同的关键字来评估布尔值.我们有YES/NOTRUE/FALSE(c99) true/false.我对BOOLbool的理解足够好,本文很好地解释了这些差异:

In Objective-C we have different keywords to evaluate boolean values. We have YES/NO, TRUE/FALSE and (c99) true/false. I understand BOOL vs bool well enough and this article explains the differences wonderfully:

http://www.bignerdranch.com/blog/bools-sharp-corners/

所以YES/NO的意思不是我的问题.我的问题是TRUE/FALSEtrue/false有什么区别?大写版本是YES/NO的别名还是(c99) true/false的别名?还是完全是另一种动物(如YES/NO)?

So what YES/NO means isn't my question. My question is, what is the difference between TRUE/FALSE and true/false? Is the uppercase version an alias for YES/NO or an alias for (c99) true/false? Or, is it another animal altogether (like YES/NO) ?

推荐答案

YESNO标识符被视为BOOL的标准Objective-C文字.在Objective-C源代码之外,通常不会找到YESNOBOOL.请注意,这些标识符实际上是在objc/objc.h中定义的宏.

The YES and NO identifiers are considered the standard Objective-C literals for BOOL. You usually won't find YES, NO, or BOOL outside of Objective-C source code. Note that these identifiers are actually macros defined in objc/objc.h.

如果您#include <stdbool.h>,则truefalse标识符是标准的C99(如您所述).请注意,由于使用的是Objective-C,即使您不知道,也可能间接包含stdbool.h.例如,Foundation.h包括CoreFoundation.h,而CoreFoundation.h包括stdbool.h.因此,在不获取truefalse的情况下,很难编译现代的iOS或Mac应用程序.

The true and false identifiers are standard C99 (as you noted), if you #include <stdbool.h>. Note that, since you're using Objective-C, you are probably including stdbool.h indirectly, even if you don't know it. For example, Foundation.h includes CoreFoundation.h, which includes stdbool.h. Thus it's pretty hard to compile a modern iOS or Mac app without getting true and false.

TRUEFALSE标识符不是标准的.它们是各个图书馆定义的历史包bag.这些库可能是在C99出现之前编写的,或者是在C99之后编写的,但旨在支持C99之前的编译器,或者仅由不了解C99布尔文字的作者编写.在Mac上,此类库的一些示例是Kerberos,XDR/RPC和ncurses.最重要的是,Mach内核标头定义了TRUEFALSE常量,并且(与stdbool.h一样)如果要构建现代的iOS或Mac应用程序,则很难避免使用这些特定的定义.

The TRUE and FALSE identifiers are not standard. They are historic baggage defined by various libraries. The libraries may have been written before the advent of C99, or written after but intended to support pre-C99 compilers, or simply written by authors ignorant of the C99 boolean literals. On the Mac, some examples of such libraries are Kerberos, XDR/RPC, and ncurses. Most importantly, the Mach kernel headers define TRUE and FALSE constants, and (as with stdbool.h) it's pretty hard to avoid these particular definitions if you're building a modern iOS or Mac app.

在我能找到的所有情况下,TRUE被定义为1(1),而FALSE被定义为0(0).

In all the cases I could find, TRUE is defined as 1 or (1) and FALSE is defined as 0 or (0).

我提到的所有库和Mach内核都早于C99,因此有理由定义它们自己的布尔常量.

All of the libraries I mentioned, and the Mach kernel, predate C99 and are thus justified in defining their own boolean constants.

Objective-C的BOOLYESNO出现在Brad Cox的

Objective-C's BOOL, YES, and NO appear in Brad Cox's Object-Oriented Programming: An Evolutionary Approach from 1991, so these identifiers are also justified.

这篇关于Objective-C TRUE/FALSE与true/false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆