views:

14285

answers:

4

Hi,

I'm new to Objective-C and 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 I there were some issues with the bool type or something ?

Can I just use bool (that seems to work) without losing speed?

Thanks for your answers

+3  A: 

Yup, BOOL is a typedef for a signed char according to objc.h.

I don't know about bool, though. That's a C++ thing, right? If it's defined as a signed char where 1 is YES/true and 0 is NO/false, then I imagine it doesn't matter which one you use.

Since BOOL is part of Objective-C, though, it probably makes more sense to use a BOOL for clarity (other Objective-C developers might be puzzled if they see a bool in use).

Jeff
_Bool is defined in C99, and in the standard header stdbool.h, the macro bool is defined (which expands to _Bool) and true/false are defined here as well.
Brian Mitchell
+2  A: 

The Objective-C type you should use is BOOL. There is nothing like a native boolean datatype, therefore to be sure that the code compiles on all compilers use BOOL. (It's defined in the Apple-Frameworks.

Georg
+24  A: 

From the definition in objc.h:

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


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

Barry Wark
"all of Apple's frameworks" - not true. Take a look at CGGeometry.h, specifically:CG_INLINE bool__CGPointEqualToPoint(CGPoint point1, CGPoint point2){ return point1.x == point2.x }
Elliot
@Elliot You are correct. Many of the C frameworks (CoreFoundation, CoreGraphics, etc.) use C99 `bool`. All of the Objective-C frameworks use `BOOL`.
Barry Wark
+2  A: 

I go against convention here. I don't like typedef's to base types. I think it's a useless indirection that removes value.

  1. When I see the base type in your source I will instantly understand it. If it's a typedef I have to look it up to see what I'm really dealing with.
  2. When porting to another compiler or adding another library their set of typedefs may conflict and cause issues that are difficult to debug. I just got done dealing with this in fact. In one library boolean was typedef'ed to int, and in mingw/gcc it's typedef'ed to a char.
Jay
Well... you *can* be expected to know the standard typedef's of your language (think `size_t`), and both `bool` (C99) and `BOOL` (ObjC) fall into that category. And if your code failed because of a change of typedef, it's your code to blame since you apparently did not handle the typedef as an opaque thing but relied on its implementation on one platform. (Nothing to be ashamed of, it happens, but it's not the typedef to blame.)
DevSolar
The "standard" typedefs don't seem to be very standard (for example for a while MS didn't support posix standards, etc). If you don't use typedefs then the problem with typedefs changing or being different on different compilers is eliminated.
Jay