Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Boolean type should I use in iOS/Objective-C?

Tags:

types

ios

It looks like there are four alternatives.

  1. BOOL

  2. bool

  3. Boolean

  4. boolean_t

Which one should I use?

There seems to be two definitions of false too.

  1. false

  2. FALSE

Which one should I use?

like image 706
Roger C S Wernersson Avatar asked Jan 08 '13 07:01

Roger C S Wernersson


People also ask

How do I set boolean value in Objective-C?

To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .

What is Boolean in Objective-C?

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.

What is the default value of BOOL in Objective-C?

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.

Why does Objective-C use yes and no?

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.


5 Answers

The most common is BOOL with YES, NO defs.

like image 106
graver Avatar answered Oct 13 '22 01:10

graver


Use BOOL type for boolean.

And use NO to set false.

like image 29
Ilanchezhian Avatar answered Oct 12 '22 23:10

Ilanchezhian


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.

like image 25
borrrden Avatar answered Oct 13 '22 00:10

borrrden


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.

like image 44
DrC Avatar answered Oct 13 '22 01:10

DrC


Better use BOOL and FALSE OR NO

Hope it helps you

like image 27
P.J Avatar answered Oct 13 '22 01:10

P.J