++ will be deprecated in swift 3
variable++ can now be written as
variable += 1
How can I rewrite ++variable
.
Please recall difference between ++variable
and variable++
syntax
Rewrite it as:
variable += 1
...exactly as the warning message suggests. This will now need to be a separate line, of course (that's the only bad thing about this change). What matters is where you put that line.
So for example
let otherVariable = ++variable // variable is a previously defined var
now becomes
variable += 1 // variable is _still_ a previously defined var
let otherVariable = variable
But on the other hand
let otherVariable = variable++ // variable is a previously defined var
now becomes
let otherVariable = variable
variable += 1 // variable is _still_ a previously defined var
Extra for experts: In the rare situation where you return variable++
— that is, you return variable
, which is in a higher scope, and then increment it — you can solve the problem like this:
defer {
variable += 1
}
return variable
You can write variable += 1
on the line above. Implement a preincrement by incrementing, before.
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