相关疑难解决方法(0)

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

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

我发现以下代码的(3) - (6)有效.
(1) - (2)不起作用,因为BOOL是公正的signed char.

(3)工作并且非常易读,但我认为bool不是客观的.
使用bool在Objective-C的代码是好的?

哪个是比较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));
Run Code Online (Sandbox Code Playgroud)

结果:

(1) 0
(2) 0
(3) 1
(4) 1
(5) 1
(6) 1
Run Code Online (Sandbox Code Playgroud)

iphone boolean compare objective-c

9
推荐指数
2
解决办法
5538
查看次数

标签 统计

boolean ×1

compare ×1

iphone ×1

objective-c ×1