0

!= 在大多数语言中意味着不相等 但是 =! 在目标 C? 我在下面的代码片段中找到了这一点。
谢谢

- (void) toggleButton: (UIButton *) aButton
{
    if ((_isOn = !_isOn))
    {
        [self setBackgroundImage:BASEGREEN forState:UIControlStateNormal];
        [self setBackgroundImage:PUSHGREEN forState:UIControlStateHighlighted];
        [self setTitle:@"On" forState:UIControlStateNormal];
        [self setTitle:@"On" forState:UIControlStateHighlighted];
    }
    else
    {
        [self setBackgroundImage:BASERED forState:UIControlStateNormal];
        [self setBackgroundImage:PUSHRED forState:UIControlStateHighlighted];
        [self setTitle:@"Off" forState:UIControlStateNormal];
        [self setTitle:@"Off" forState:UIControlStateHighlighted];
    }

    [self relaxButton:self];
}
4

3 回答 3

7

A =!B 基本上是一个布尔值,表示“将 A 设置为 B 的对面”。

例如,您可以这样做:

BOOL a = NO;
BOOL b = !a;

然后 b 将是YES

你的这行代码基本上是翻转 BOOL is_On 的状态,然后如果新状态为 YES 则执行 if 语句块,否则执行 else 块。

于 2013-07-30T00:19:14.373 回答
1

!是基本的布尔运算符之一。与||&&一起!用于操作布尔值。因为布尔值只能是真或假,所以布尔运算符很容易理解。

1) not 运算符 ( !) 用于反转布尔变量的状态。真变假,假变真。

 BOOL a = NO;
 a = !a;
 if (a) {
 ...
 // will execute because a is now YES, or true
 }

2) and 运算符 ( &&) 用于两个布尔值。如果要比较的两个值都为真,则返回真。如果任何被比较的值是假的,它返回假

BOOL a = NO;
BOOL b = YES;
if (a && b) {
...
// will not execute because NO && YES is NO
}

a = YES;
if (a && b) {
...
// will execute because YES && YES is YES
}

3) or 运算符 ( ||) 也用于两个布尔变量。如果有问题的布尔值之一为真,则返回真。

BOOL a = YES;
BOOL b = NO;
if (a || b) {
...
// will execute because YES || NO is YES
}

在考虑布尔运算符时,真值表是一种宝贵的资源

true || true = true
true || false = true
false || true = true
false || false = false

true && true = true
true && false = false
false && true = false
false && false = false

!true = false
!false = true
于 2013-07-30T02:30:46.923 回答
0

你误读了。不是_isOn =! _isOn,是_isOn = !_isOn。注意 和 之间的=空格!。一个感叹号是逻辑否定,即它切换 的值_isOn并将结果分配回_isOn

于 2013-07-30T00:21:18.230 回答