Is there any difference between bool, Boolean, and BOOL in Objective-C?
Solution 1
Boolean
is an old Carbon keyword (historic Mac type), defined as an unsigned char
. BOOL
is an Objective-C type defined as signed char
. bool
is a defined version of the _Bool
standard C type. It's defined as an int
. Use BOOL
.
Edit (2019): Apple talks about the underlying implementation of BOOL
in some new documentation. Basically, on macOS, BOOL
is still ultimately a signed char
, but on iOS and related platforms, it a native C bool
underneath.
Solution 2
I don't want to take away from @JonShier's useful answer, but I've more to add than fits well in a comment...
bool
Introduced to standard C in the C99 spec. (The C99 standard was published in 1999, but it took some years after that to become widespread in use.) Prior to that, "plain" C had no built-in Boolean type, so libraries that built on top of C often defined their own. (And often continued using their own types for source/binary compatibility even after they embraced C99 compilers.)
Use this if you're writing ISO C and aren't working in the context of higher level libraries with their own Boolean types.
Boolean
Defined by Carbon (the early-OSX-days compatibility bridge from the even older Mac Toolbox), which you might still see in some projects (due to transitive #include
of headers that are really only around for compatibility with really old source code).
Don't use this.
BOOL
Defined by ObjC because NeXTSTEP needed its own Boolean type back in 1988. (The oldest objc.h
I can find on my office bookshelf dates to 1992 and includes a definition of BOOL
.)
ObjC BOOL
has often been defined as typedef signed char
, meaning that it can hold more values than just YES
(1) and NO
(0). That can be a problem if you aren't careful. (Why even do that? Because if a type is one bit wide, it's hard to pack into well-aligned memory for good performance.)
However, in iOS 64-bit (including tvOS) and watchOS, the compiler defines OBJC_BOOL_IS_BOOL
, which makes ObjC BOOL
just an alias for C99 bool
. That means the language/compiler ensures that nonzero values are always stored as 1, so you don't have the issues that come from typedef signed char BOOL
. (Still gotta worry about them on macOS or 32-bit iOS, though.)
TLDR
If you're working in ObjC with ObjC frameworks (like Cocoa, UIKit, etc), you should use BOOL
for consistency with the APIs you're interacting with. (Besides, YES
and NO
are much louder than true
and false
, and it's good to be emphatic when you're talking about absolute truth, right?)
Frost
Updated on June 17, 2022Comments
-
Frost almost 2 years
I know BOOL is actually a typedef of signed char, but what about Boolean?
What is the difference between bool, Boolean and BOOL?
-
bobDevil almost 14 years
Boolean
is used in CoreFoundation (which Carbon is mostly built on top of now), so while you should use BOOL in objective-C, when dealing with any CoreFoundation APIs, Boolean is still in use. -
Frost almost 14 yearsOh, I see, so I should use BOOL for common use and Boolean in CF. Thanks!
-
Jon Shier almost 14 yearsOops, you're right. CoreFoundation does define a version of
Boolean
, but that's separate from Carbon's definition. See MacOSTypes.h vs. OSTypes.h. And on 64-bit systems that don't use C99, CoreFoundation'sBoolean
is anunsigned char
. -
Jon Shier almost 14 yearsFrom objc.h:
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" // even if -funsigned-char is used.
So BOOL for Objective-C, Boolean for everything else. -
TOMKA almost 14 yearsIf
stdbool.h
is included in C99, thenbool
is a macro expanding to_Bool
, which is a built-in true Boolean type representing either 0 or 1, it is not anint
. -
zwol almost 14 yearsNowadays, one should always use
bool
(includingstdbool.h
if not coding in [Objective] C++) except to interoperate with existing APIs that use one of the older types. Using a real Boolean type (can only take the values 0 and 1, any nonzero integer converts totrue
automagically) obliterates an entire class of possible bugs. -
Steven Kramer almost 10 yearsZack, this also introduces a class of bugs called pointer to bool conversions. Still prefer bool over BOOL though.
-
DawnSong almost 6 years@zwol is right.
BOOL v = 2; NSLog(@"%d", v);
outputs "1", not "2". Reference to Apple documentation -
schellsan over 2 yearsIt seems BOOL is a C99 bool on arm64 macos (Apple Silicon) as well. So maybe it has more to do with the chip architecture and less to do with the operating system? Here's a link to a GH issue investigation github.com/SSheldon/rust-objc/issues/110