Reputation: 61
I'm programming in Swift 5.0 an app to move my character around a world and I've some problems with the NOT (!) operator in the if
condition body.
I wrote:
if character.!isBlocked {
character.moveForward()
}
but the compiler say: Error: ".!" it's not a binary operator
Instead, if I wrote
if character.isBlocked {
character.turnBack()
}
it works perfectly. Its possible to use a negative clause as if
condition clause?
Upvotes: 1
Views: 171
Reputation: 31645
If you are aiming to validate as "if not", then you should implement it as:
if !character.isBlocked {
character.moveForward()
}
The logical NOT !
operator is a unary prefix operator:
Unary: it is for a single target.
Prefix: it should be before the target.
So, why it should be before character.isBlocked
but not isBlocked
?
Because character.isBlocked
is the boolean to evaluate, isBlocked
is the property name. The correct syntax is to use !
to evaluate the whole thing (Boolean value).
Upvotes: 1