13

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

sage444
  • 5,661
  • 4
  • 33
  • 60
SlopTonio
  • 1,105
  • 1
  • 16
  • 39
  • Makes no difference. It isn't what you say, it's when you say it. If _you_ understand the difference between `++variable` and `variable++`, this is obvious. If not, please read my now-oh-so-tediously-revised answer. – matt Mar 23 '16 at 17:54
  • Read this http://stackoverflow.com/questions/17366847/what-is-the-difference-between-pre-increment-and-post-increment-in-the-cycle-fo?answertab=votes#tab-top. Should apply in Swift too. – Akaino Mar 23 '16 at 18:04
  • Read this: http://stackoverflow.com/questions/36173096/is-deprecated-it-will-be-removed-in-swift-3 – Zigii Wong Apr 13 '16 at 07:36
  • @matt: I still don't get it. Why did you vote to "close as unclear what you're asking"? – You answered the question 2 minutes after it were asked, so it must have been clear to you. – Martin R Oct 08 '17 at 11:11

2 Answers2

33

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
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I think author asks about different syntax `++var` and `var++`. How first variant should be fixed? – sage444 Mar 23 '16 at 17:49
  • 6
    @sage444 Same way. It's as plain as plain can be. The problem is that `++` is not; that's why it's being abolished. Most people don't even understand the difference between preincrement and postincrement. So now you just increment — at the moment when it makes sense to do so. – matt Mar 23 '16 at 17:50
  • It gets a bit more complicated when you're using a postincrement expression as a return value `return index++` ;) – vadian Mar 23 '16 at 18:03
  • 2
    @vadian, I think that's Matt's point. The behavior of `return index++` is subtle and likely lost on naive developers. In the interest of clarity, subtle is bad. – Duncan C Mar 23 '16 at 18:35
  • Out of scope question, why did they depreciate it? – JustADev Mar 25 '16 at 17:30
  • 2
    @OmarBizreh https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md – matt Mar 25 '16 at 17:54
  • @vadian added a postscript to my answer, explaining how to do `return index++` – matt Mar 25 '16 at 17:57
  • Wow, this is pretty nifty :-) – vadian Mar 25 '16 at 18:05
  • @vadian I thought you knew this and were just yanking my chain. :) – matt Mar 25 '16 at 18:07
  • I must admit that `defer` didn't come at all to my mind – vadian Mar 25 '16 at 18:17
1

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

Tommy
  • 99,986
  • 12
  • 185
  • 204