I have the following code
var column = 0
column = column >= 2 ? 0 : ++column
Since 2.2 I get a depreciation warning, any ideas how I can fix this?
I have this solution:
if column >= 2 {
column = 0
} else {
column += 1
}
But this isn't really nice.
Swift has a rarely used operator called the ternary operator. It works with three values at once, which is where its name comes from: it checks a condition specified in the first value, and if it's true returns the second value, but if it's false returns the third value.
The expression represented by the expression pattern is compared with the value of an input expression using the Swift standard library ~= operator. The matches succeeds if the ~= operator returns true . By default, the ~= operator compares two values of the same type using the == operator.
How about:
column = (column >= 2) ? 0 : column+1
It looks like you might be doing something like clock arithmetic. If so, this gets the point across better:
column = (column + 1) % 2
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