5

Possible Duplicates:
Objective-C : BOOL vs bool
Is there any difference between BOOL and Boolean in Objective-C?

I noticed from the autocomplete in XCode that there is a bool and a BOOL in Objective-C. Are these different? Why are there two different kinds of bool?

Are they interchangeable?

Community
  • 1
  • 1
node ninja
  • 31,796
  • 59
  • 166
  • 254

4 Answers4

10

Yes they are different.

  • C++ has bool, and it is a true Boolean type. It is guaranteed to be 0 or 1 within integer context.
  • C99 has _Bool as a true Boolean type, and if <stdbool.h> is included, then bool becomes a preprocessor macro for _Bool (this header also defines true and false as preprocessor macros for 1 and 0 respectively).
  • Cocoa has BOOL as a type, but it is just a typedef for signed char. It can represent more values than just 0 or 1.
  • Carbon has Boolean as a type, but it is just a typedef for unsigned char. Like, Cocoa's BOOL it can represent more values than just 0 or 1.

For Cocoa and Carbon's “Boolean” types, they should be thought of as zero meaning false, and any non-zero value meaning true.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
3

BOOL is defined in Objective-C as typedef signed char BOOL, while bool is the datatype defined in C99.

apaderno
  • 28,547
  • 16
  • 75
  • 90
3

You should use bool unless you need to interoperate with code that uses BOOL, because bool is a real Boolean type and BOOL isn't. What do I mean "real Boolean type"? I mean that code like this does what you expect it to:

#define FLAG_A 0x00000001
#define FLAG_B 0x00000002
...
#define FLAG_F 0x00000020
struct S
{
    // ...
    unsigned int flags;
};

void doSomething(S* sList, bool withF)
{
    for (S* s = sList; s; s = s->next)
    {
        if ((bool)(s->flags & FLAG_F) != withF)
            continue;
        // actually do something
    }
}

because (bool)(s->flags & FLAG_F) can be relied upon to evaluate to either 0 or 1. If that were a BOOL instead of a bool in the cast, it wouldn't work, because withF evaluates to 0 or 1, and (BOOL)(s->flags & FLAG_F) evaluates to 0 or the numeric value of FLAG_F, which in this case is not 1.

This example is contrived, yeah, but real bugs of this type can and do happen all too often in old code that doesn't use the C99/C++ genuine boolean types.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 3
    For Objective-C, this is not good advice and is not the convention. – Grant Paul Jul 30 '10 at 23:16
  • If the convention is to use the more error-prone programming style, then the convention is wrong IMO. – zwol Jul 30 '10 at 23:19
  • 2
    chpwn: I don't see how conforming to the convention conflicts with the advice in the answer; he says “unless you need to interoperate with code that uses `BOOL`”, and all Cocoa code is such code. ☺ Moreover, if you read his demonstration, `bool` does look really handy. – Peter Hosey Jul 30 '10 at 23:49
  • What would be wrong with `if((s->flags & FLAG_F) && !withF)`? I agree overall that a proper boolean type should be used in favour of an integer type. – dreamlax Jul 31 '10 at 02:33
  • 1
    That doesn't do quite the same thing! The correct version of my code, if BOOL were in use, would be `if(!!(s->flags & FLAG_F) != !!withF)`. But the larger point is, yes, you *can* always fix the code at the point where the bug is, but if you use a proper boolean type you don't have to worry about it. – zwol Jul 31 '10 at 16:48
2

BOOL is actually a signed char (thanks Yuji), while bool is a true boolean from the ISO C99 standard.

See here: http://iosdevelopertips.com/objective-c/of-bool-and-yes.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
Don
  • 3,654
  • 1
  • 26
  • 47
  • Strange, in Windows, `BOOL` is actually an `int`! – Blindy Jul 30 '10 at 22:53
  • In C++, `char`, `signed char` and `unsigned char` are all different. So, in Objective-C++, `BOOL` is not a `char`; it's a `signed char`. – Yuji Jul 30 '10 at 23:14
  • You're right - I'll edit my answer since I see it's grammatically incorrect anyway. – Don Aug 02 '10 at 14:42