Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Objective-c, safe and good way to compare 2 BOOL values?

I want to compare 2 BOOL values in objective-c.

I found out that (3)-(6) of the following code works.
(1)-(2) doesn't work because BOOL is just signed char.

(3) works and is very readable but I think bool isn't objective-c.
Using bool in objective-c code is good?

Which is the safe and good way to compare 2 BOOL values in objective-c?
Are there other better ways to compare?

BOOL b = YES;
BOOL c = 2;

NSLog(@"(1) %d", b == c); // not work
NSLog(@"(2) %d", (BOOL)b == (BOOL)c); // not work
NSLog(@"(3) %d", (bool)b == (bool)c);
NSLog(@"(4) %d", !b == !c);
NSLog(@"(5) %d", !!b == !!c);
NSLog(@"(6) %d", (b != 0) == (c != 0));

results:

(1) 0
(2) 0
(3) 1
(4) 1
(5) 1
(6) 1
like image 830
js_ Avatar asked Jun 21 '12 08:06

js_


People also ask

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.

Can bool be nil Objective C?

By default, a bool value is set to 0 in Objective-C, so you don't need to check if your bool value is nil anytime.

Is bool 1 True or false?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.

How many different values does the bool type have?

A Boolean data type has one of two possible values (usually denoted true and false), intended to represent the two truth values of logic and Boolean algebra.


2 Answers

Comparing two boolean values should be handled with an XOR operation.

Trying to compare the two booleans directly is a misuse of the fundamentals of Boolean Algebra: http://en.wikipedia.org/wiki/Boolean_algebra_(logic)

When you are doing

BOOL a = (b == c);

then this value may return false even if both b and c are true. However the expression b && c will always return YES if both b and c are true, i.e. greater than 0, and NO otherwise.

Instead this is what you are actually trying to do:

BOOL xor = b && !c || !b && c;
BOOL equal = !xor;

equivalent with

BOOL equal = !(b && !c || !b && c);

or

BOOL equal = (b && c) || (!b && !c)

If you have to spend time making sure that your BOOL values are normalized (i.e. set to either 1 or 0) then your doing something wrong.

like image 100
jake_hetfield Avatar answered Sep 30 '22 19:09

jake_hetfield


It's perfectly valid to use bool in Objective-C as it's part of the C99 standard (§7.16). In my opinion it's also the best way to handle safe comparisons of boolean types.

The only reason not to use bool everywhere is that BOOL is omnipresent in Objective-C and the frameworks.

like image 23
Nikolai Ruhe Avatar answered Sep 30 '22 18:09

Nikolai Ruhe