在Objective-c中,比较2个BOOL值的安全且好的方法?

时间:2021-10-26 19:33:50

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

我想比较objective-c中的2个BOOL值。

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

我发现以下代码的(3) - (6)有效。 (1) - (2)不起作用,因为BOOL只是签名字符。

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

(3)工作并且非常易读,但我认为bool不是客观的。在objective-c代码中使用bool是好的吗?

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

哪个是比较objective-c中的2个BOOL值的安全且好的方法?还有其他更好的比较方法吗?

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

6 个解决方案

#1


5  

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.

在Objective-C中使用bool是完全有效的,因为它是C99标准(第7.16节)的一部分。在我看来,这也是处理布尔类型安全比较的最佳方法。

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

不在任何地方使用bool的唯一原因是BOOL在Objective-C和框架中无处不在。

#2


9  

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

应该使用XOR运算来处理两个布尔值。

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

试图直接比较两个布尔值是滥用布尔代数的基本原理: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.

那么即使b和c都为真,这个值也可能返回false。然而,如果b和c都为真,即大于0,则表达式b && c将始终返回YES,否则返回NO。

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.

如果你必须花时间确保你的BOOL值被标准化(即设置为1或0),那么你做错了。

#3


4  

apart from the other answers, I would like to note that comparing bools for equality is not a very common operation. BOOL is not a type, it's just a macro which hides the fact that booleans are only integers. For every boolean operation, you should rather use programming structures and operators that can handle integers as booleans correctly:

除了其他答案之外,我想指出,比较bools的平等并不是一个非常常见的操作。 BOOL不是一个类型,它只是一个宏,它隐藏了布尔只是整数的事实。对于每个布尔操作,您应该使用可以正确处理整数的编程结构和运算符:

e.g: if (condition1 == NO) {} should be if (!condition1) {}

例如:if(condition1 == NO){}应该是if(!condition1){}

if (condition1 == condition2) {}
can be
if ((condition1 && condition2) || (!condition1 && !condition2)) {}
or better
BOOL newCondition = (condition1 && condition2) || (!condition1 && !condition2); if (newCondition) {}

if(condition1 == condition2){}可以是if((condition1 && condition2)||(!condition1 &&!condition2)){}或更好BOOL newCondition =(condition1 && condition2)|| (!condition1 &&!condition2); if(newCondition){}

The shortest way to write a condition doesn't have to be the best way.

写条件的最短方式不一定是最好的方法。

#4


1  

Convert your number to a valid BOOL, by answering what do you mean by "2"

通过回答“2”的含义,将您的号码转换为有效的BOOL

in your context, is 2 = YES

在您的上下文中,是2 =是

Int number = 2;
BOOL c = (number == 2); //2 is YES

is > 0 = YES

是> 0 =是

Int number = 2;
BOOL c = (number > 0); //2 is YES

It depends manly on what is TRUE and what is False in your application

这取决于你的应用程序中什么是TRUE以及什么是False

#5


1  

From objc.h:

typedef signed char     BOOL; 
....

#define YES             (BOOL)1
#define NO              (BOOL)0

Apparently, BOOL is signed char, so it is true that you can assign number to variable of BOOL type, which will mess up the comparison (since the comparison is integer comparison).

显然,BOOL是签名字符,所以你可以为BOOL类型的变量赋值,这会搞砸比较(因为比较是整数比较)。

I think your (4) method using negation to convert arbitrary integral value to 0 or 1 is a good short way to safely compare the logical value of 2 BOOL.

我认为你的(4)方法使用否定将任意积分值转换为0或1是安全地比较2 BOOL的逻辑值的一个很好的简短方法。

#6


-1  

If you assign the integer 2 to a variable of type BOOL, your code is broken. Whatever you get after that, it's what you deserve.

如果将整数2分配给BOOL类型的变量,则代码将被破坏。无论你得到什么,这都是你应得的。

Therefore, the only sensible way to compare two BOOLs a and b is

因此,比较两个BOOL a和b的唯一合理方法是

if (a == b) ...
if (a != b) ...

#1


5  

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.

在Objective-C中使用bool是完全有效的,因为它是C99标准(第7.16节)的一部分。在我看来,这也是处理布尔类型安全比较的最佳方法。

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

不在任何地方使用bool的唯一原因是BOOL在Objective-C和框架中无处不在。

#2


9  

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

应该使用XOR运算来处理两个布尔值。

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

试图直接比较两个布尔值是滥用布尔代数的基本原理: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.

那么即使b和c都为真,这个值也可能返回false。然而,如果b和c都为真,即大于0,则表达式b && c将始终返回YES,否则返回NO。

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.

如果你必须花时间确保你的BOOL值被标准化(即设置为1或0),那么你做错了。

#3


4  

apart from the other answers, I would like to note that comparing bools for equality is not a very common operation. BOOL is not a type, it's just a macro which hides the fact that booleans are only integers. For every boolean operation, you should rather use programming structures and operators that can handle integers as booleans correctly:

除了其他答案之外,我想指出,比较bools的平等并不是一个非常常见的操作。 BOOL不是一个类型,它只是一个宏,它隐藏了布尔只是整数的事实。对于每个布尔操作,您应该使用可以正确处理整数的编程结构和运算符:

e.g: if (condition1 == NO) {} should be if (!condition1) {}

例如:if(condition1 == NO){}应该是if(!condition1){}

if (condition1 == condition2) {}
can be
if ((condition1 && condition2) || (!condition1 && !condition2)) {}
or better
BOOL newCondition = (condition1 && condition2) || (!condition1 && !condition2); if (newCondition) {}

if(condition1 == condition2){}可以是if((condition1 && condition2)||(!condition1 &&!condition2)){}或更好BOOL newCondition =(condition1 && condition2)|| (!condition1 &&!condition2); if(newCondition){}

The shortest way to write a condition doesn't have to be the best way.

写条件的最短方式不一定是最好的方法。

#4


1  

Convert your number to a valid BOOL, by answering what do you mean by "2"

通过回答“2”的含义,将您的号码转换为有效的BOOL

in your context, is 2 = YES

在您的上下文中,是2 =是

Int number = 2;
BOOL c = (number == 2); //2 is YES

is > 0 = YES

是> 0 =是

Int number = 2;
BOOL c = (number > 0); //2 is YES

It depends manly on what is TRUE and what is False in your application

这取决于你的应用程序中什么是TRUE以及什么是False

#5


1  

From objc.h:

typedef signed char     BOOL; 
....

#define YES             (BOOL)1
#define NO              (BOOL)0

Apparently, BOOL is signed char, so it is true that you can assign number to variable of BOOL type, which will mess up the comparison (since the comparison is integer comparison).

显然,BOOL是签名字符,所以你可以为BOOL类型的变量赋值,这会搞砸比较(因为比较是整数比较)。

I think your (4) method using negation to convert arbitrary integral value to 0 or 1 is a good short way to safely compare the logical value of 2 BOOL.

我认为你的(4)方法使用否定将任意积分值转换为0或1是安全地比较2 BOOL的逻辑值的一个很好的简短方法。

#6


-1  

If you assign the integer 2 to a variable of type BOOL, your code is broken. Whatever you get after that, it's what you deserve.

如果将整数2分配给BOOL类型的变量,则代码将被破坏。无论你得到什么,这都是你应得的。

Therefore, the only sensible way to compare two BOOLs a and b is

因此,比较两个BOOL a和b的唯一合理方法是

if (a == b) ...
if (a != b) ...