tags:

views:

333

answers:

3

I already found out that bool is a C-Type while BOOL is an Objective-C Type. bool can be true or false and BOOL can be YES or NO

For beginners it can be hard to distinguish these types. Is there anything bad that can happend if I use bool instead of BOOL?

+3  A: 

While you might be able to get away with using bool instead of BOOL for your own needs, you might run into problems with passing a 'bool' to something else (like a public API method). You should simply stick with BOOL when writing Objective-C code, mostly for future-proofing.

But I don't think anything will blow up if you do, it's just highly not recommended and doesn't follow any of Objective-C's conventions.

As a side note, BOOL's YES or NO format is kind of a suggestion, I've had the bad habit of setting a BOOL to TRUE or FALSE (notice the all uppercase) and it's no problem. You can also use 1 or 0.

These are all valid:

BOOL myBool;
myBool = YES;   // true
myBool = TRUE;  // true
myBool = 1;     // true
myBool = NO;    // false
myBool = FALSE; // false
myBool = 0;     // false
Neil Daniels
Technically, myBool = 'Z' is equally valid, but it's against the semantics of BOOL to use anything but YES and NO.
Chuck
A: 

Compatibility problems are very unlikely in common cases (setting it to YES/1 or 0/NO).

I suggests you to refer documentation for differences in other cases: http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/c_ref/BOOL

ybart
+3  A: 

BOOL is a signed char, while bool is a int (in fact, it's typedef'd as such in Darwin when compiling with pre-C99 standards). From there, the usual consideration when promoting/demoting between integer types can be followed.

Graham Lee