All kinds of Booleans in ObjectiveC

ios, objective-c

Solution

iOS and OS X are mostly made of Cocoa, which use the boolean type `BOOL` with values `YES`/`NO`.

`bool`

- Defined by C++.

- A true boolean, guaranteed to be 0 or 1.

`_Bool`

- Defined by C99.

- A true boolean, guaranteed to be 0 or 1.

- If stdbool.h is included, bool is #defined as _Bool.

`BOOL`

- Defined by the Objective-C runtime at `/usr/include/objc/objc.h`.

- A `signed char` in 32 bit. Values may be `YES` (0x01), `NO` (0x00), or anything in the range -127 to 128. `YES`/`NO` are defined at `<Foundation/NSObjCRuntime.h>`.

- A `bool` in 64 bits, guaranteed to be 0 or 1.

`Boolean`

- Defined by Carbon at CFBase.h.

- An unsigned char.

- Values may be `TRUE` (0x01), `FALSE` (0x00), or anything in the range -127 to 128.

`boolean_t`

- Defined by `/usr/include/mach/i386/boolean.h`

- An int in x32 or unsigned int in x64.

For non true boolean types:

- Any non zero value is treated as true in logical expressions.

- If you cast to a boolean types with less range than the casted type, only the lower bytes are used.

Cases where one type or another makes a difference are hard to imagine. There are several cases where casting to BOOL may bite you, and some rare situations (eg: KVO converts BOOL to a NSNumber, and bool to a CFBoolean). If anything, when you consistently use BOOL, you are covered in case Apple changes its definition.

Problem

Possible Duplicate: What values should I use for iOS boolean states? I believe there are something like 5 boolean types in iOS environment (which comes from C, C++ and Objective C). - _Bool - bool - BOOL - boolean_t - Boolean And there are at least four pairs of values for them: - true, false - TRUE, FALSE - YES, NO - 1, 0 Which one do you think is the best (style wise) to use for iOS Objective C development? Update 1 I mentioned a type "boolean". It looks like it doesn't exist. I removed it from the list and added _Bool. I am aware of typedef's for these types and values. The question is about style differences.

Original source

Related problems