TalkingCode
TalkingCode

Reputation: 13567

Objective-C : Mistake BOOL and bool?

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?

Upvotes: 2

Views: 1148

Answers (3)

user23743
user23743

Reputation:

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.

Upvotes: 5

user150168
user150168

Reputation:

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

Upvotes: 0

Neil
Neil

Reputation: 1853

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

Upvotes: 4

Related Questions