3

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.

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • 2
    Generally, one should use BOOL/YES/NO (and nil) when writing Objective-C code. But if you're writing what is basically embedded C/C++ code you may prefer to use whatever C convention you learned a your mother's knee. – Hot Licks Jan 22 '13 at 17:39
  • Where did you find the `boolean` type? – Jano Jan 22 '13 at 18:45

6 Answers6

5

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.

Jano
  • 62,815
  • 21
  • 164
  • 192
3

Use BOOL and YES/NO in Objective-C code. That's expected in Objective-C code and how the Objective-C headers define the data type. (Note that you may occasionally deal with other type/values, such as when checking the "truthiness" of pointers or when dealing with, e.g., C++ code, but Objective-C generally uses BOOL and YES/NO for boolean data types and values.)

mipadi
  • 398,885
  • 90
  • 523
  • 479
2

It's basically absolutely indifferent. In practice, I bet they're even declared the very same way: typedef signed char BOOL;, typedef signed char Boolean;, etc.

So they're practically compatible and equivalent; the best approach is, however, to respect the type methods expect and return, so write

[object someObjectiveCMethod:YES];

instead of

[object someObjectiveCMethod:TRUE];

and

CFWhateverSetBooleanProperty(true);

instead of

CFWhateverSetBooleanProperty(YES);
2

Use ObjC's BOOL for ObjC code. Use the others where they're "native". Anything else will tend to stand out as "unusual" and worthy of extra care when reading the code, which is a property that should be reserved for code that actually is unusual and worthy of extra care.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
0

BOOL with YES and NO.

However BOOL is signed char so YES equals to 1, and NO equals to 0.


In objc.h they are defined as:

typedef signed char BOOL; 

And

#define YES ((BOOL)1)
#define NO  ((BOOL)0)
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

You could use each one if wasn't for the fact that who reads the code may not know this type. So for convention, use BOOL which could have a value of YES (1) or NO .

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187