It looks like there are four alternatives.
BOOL
bool
Boolean
boolean_t
Which one should I use?
There seems to be two definitions of false too.
false
FALSE
Which one should I use?
To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .
Boolean, in Objective-C, is a hold over data type from the C language. Both in C, and hence in Objective-C, 0 (zero) is treated as “false” and 1 (one) as “true”. C has a Boolean data type, bool (note: the lowercase), which can take on the values of true and false.
It is initialized to garbage. However, for a BOOL ivar, it will be initialized to NO , as the whole instance is filled with 0 on initialization. (Note: When ARC is enabled, local object pointers will always be have a default value nil , but local variables of non-object types like BOOL are still initialized to garbage.
It's just syntax, there's no technical reason for it. They just use YES/NO for their BOOL instead of true/false like c++ does.
The most common is BOOL
with YES
, NO
defs.
Use BOOL
type for boolean.
And use NO
to set false.
BOOL
is the one offered by objective-C, so stick with it unless it becomes a problem (this is a very rare case, but it happened to me once). Also, there are more definitions for true and false: YES
and NO
are the most objective-C like. They are defined as the clang literals, so it is best to use them.
It will vary on the API you are calling. Each API will have its own convention. For most of the Apple Obj-C stuff, it is BOOL with YES/NO defined. However, you may end up working with another library that has its own convention (e.g. boolean_t) - in calls to that library match the convention. For your own stuff, I'd stick to the Obj-C method.
Better use BOOL
and FALSE
OR NO
Hope it helps you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With