Jason Caldwell Jason Caldwell - 2 months ago 7
Objective-C Question

Objective-C TRUE/FALSE vs true/false

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/

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
) ?

Answer

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.

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.

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.

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

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

Comments