Francescu Francescu - 1 year ago 230
Objective-C Question

Objective-C : BOOL vs bool

I saw the "new type"


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?

Answer Source

From the definition in objc.h:

typedef bool BOOL;
typedef signed char BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.

#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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download