I have the following code:
switch self.score
{
case 1:
self.score = self.score - 2
case -10...-10000: // ! Expected expression after unary operator
println("lowest score")
self.score = -10
default:
self.score = self.score - 1
}
I have also tried case -1000...-10:
. Both get the same error ! Expected expression after unary operator
.
What I would really like to do is case <= -10:
, but I can't figure out how to that without getting this error Unary operator cannot be separated from its operand
.
What am I not understanding?
No. Because the range formula subtracts the lowest number from the highest number, the range is always zero or a positive number.
The range of any data set is calculated by subtracting the lowest value from the highest value.
A uint8 data type contains all whole numbers from 0 to 255. As with all unsigned numbers, the values must be non-negative.
As the name suggests, the positiveornegativenumber variable can contain either a positive or negative number. As a result, if said variable is positive, the for loop works. If it is negative, the loop exits immediately because i is already larger than negativeorpositivenumber.
In the context of a switch case, a ... b
is a "closed interval" and the start
must be less or equal to the end of the interval. Also a plus or minus sign must be
separated from ...
by a space (or the number enclosed in parentheses), so both
case -10000...(-10):
case -10000 ... -10:
work.
case <= -10:
can be written in Swift
using a "where clause":
case let x where x <= -10:
Starting with Swift 4 this can be written as a “one-sided range expression”:
case ...(-10):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With