Reputation: 5152
!= in most language means not equal
But how about =! in objective C? I found this in the code snippet below.
Thanks
- (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];
}
Upvotes: 0
Views: 1779
Reputation: 6831
!
is one of the fundamental boolean operators. Along with ||
and &&
, !
is used to manipulate boolean values. Because booleans can only be true or false, boolean operators are easy to understand.
1) The not operator (!
) is used to reverse the state of a boolean variable. True becomes false and false becomes true after not.
BOOL a = NO;
a = !a;
if (a) {
...
// will execute because a is now YES, or true
}
2) The and operator (&&
) is used on two boolean values. It return true if both values being compared are true. It returns false if any of the values being compared are false
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) The or operator (||
) is also used on two boolean variables. It return true if either one of the booleans in question is true.
BOOL a = YES;
BOOL b = NO;
if (a || b) {
...
// will execute because YES || NO is YES
}
A truth table is a valuable rescource when thinking about boolean operators
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
Upvotes: 1
Reputation: 19855
You're misreading it. It's not _isOn =! _isOn
, it's _isOn = !_isOn
. Notice the space between the =
and the !
. A single exclamation is logical negation, i.e., it is toggling the value of _isOn
and assigning the result back to _isOn
.
Upvotes: 0
Reputation: 5312
A =!B is basically a boolean saying "set A to the opposite of B".
For example, you could do:
BOOL a = NO;
BOOL b = !a;
and b would then be YES
.
Your line of code is basically flipping the state of the BOOL is_On, and then the if statement block is being executed if the new state is YES, otherwise the else block is being executed.
Upvotes: 7