不推荐使用++,它将在swift 3中删除 [英] ++ is deprecated it will be removed in swift 3
问题描述
++将在swift 3中弃用
++ will be deprecated in swift 3
变量++
现在可写为
variable++ can now be written as
variable += 1
如何重写 ++变量
。
请回忆一下 ++变量
和变量++
之间的区别语法
Please recall difference between ++variable
and variable++
syntax
推荐答案
将其重写为:
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.
所以例如
let otherVariable = ++variable // variable is a previously defined var
现在变为
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
现在变为
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
这篇关于不推荐使用++,它将在swift 3中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!