Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c boolean values

I was wondering what the difference was between the following values in objective c:

TRUE(uppercase) - true(lowercase) - yes
FALSE(uppercase) - false(lowercase) - no

they are colored differently in the IDE, are there different situations when you would use the different boolean values?

Thanks

like image 742
Dave C Avatar asked Aug 10 '10 18:08

Dave C


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] . But there's little reason to do that. Key-Value Coding is a roundabout way to accomplish this.

What is boolean in Objective-C?

A boolean is binary value expressed in Objective-C as the primitive type BOOL which can hold either YES or NO (true or false) values. Their primary use in programming is in decision making.

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.

What are boolean values in C?

A boolean is a data type in the C Standard Library which can store true or false . Every non-zero value corresponds to true while 0 corresponds to false . The boolean works as it does in C++. However, if you don't include the header file​ stdbool.


1 Answers

These values are colored differently, as they are used in two different types - BOOL and bool and are different language constructs.

BOOL is a macro re-definition of signed char, which means it can have more than two values, and while the mapping is NO/FALSE == 0, YES/TRUE == 1, you have to be careful when writing boolean expressions and treat any non-zero value as true. Values that can be assigned to BOOL are defined as macros and are colored accordingly.

Meanwhile, bool on the other hand is a true boolean type and can have only two values - true and false. Its values are native language constructs and are colored as such.

Here are some other SO discussions of this topic:
Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?
Objective-C : BOOL vs bool

like image 160
Franci Penov Avatar answered Sep 28 '22 09:09

Franci Penov