Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

++ is deprecated it will be removed in swift 3 [closed]

Tags:

ios

swift

++ 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

like image 857
SlopTonio Avatar asked Mar 23 '16 17:03

SlopTonio


2 Answers

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
like image 179
matt Avatar answered Nov 17 '22 12:11

matt


You can write variable += 1 on the line above. Implement a preincrement by incrementing, before.

like image 26
Tommy Avatar answered Nov 17 '22 12:11

Tommy