Task: Return current value of variable and increase itself after go out function.
Java implementation
class A {
public int x = 0;
public int changeVal() {
return x++;
}
}
Usage:
A a = new A();
a.x; //output is 0
a.changeVal(); //output is 0
a.x; //output is 1
The same implementation converted to Swift:
class A {
var x : Int = 0
func changeVal() -> Int {
let t = x
x += 1
return t
}
}
Is there any shorter way to achieve this without those two lines?
let t = x
x += 1