Objective-C : BOOL vs bool

boolean, c, objective-c, types

Solution

From the definition in `objc.h`:

#if (TARGET_OS_IPHONE && __LP64__)  ||  TARGET_OS_WATCH
typedef bool BOOL;
#else
typedef signed char BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.
#endif

#define YES ((BOOL)1)
#define NO  ((BOOL)0)

So, yes, you can assume that BOOL is a char. You can use the (C99) `bool` type, but all of Apple's Objective-C frameworks and most Objective-C/Cocoa code uses BOOL, so you'll save yourself headache if the typedef ever changes by just using BOOL.

Problem

I saw the "new type" `BOOL` (`YES`, `NO`). I read that this type is almost like a char. For testing I did : ``` NSLog(@"Size of BOOL %d", sizeof(BOOL)); NSLog(@"Size of bool %d", sizeof(bool)); ``` Good to see that both logs display "1" (sometimes in C++ bool is an int and its sizeof is 4) So I was just wondering if there were some issues with the bool type or something ? Can I just use bool (that seems to work) without losing speed?

Original source