In swift 2.2, We could mutate a struct or enum within a closure, when it was inside a mutating function. But in swift 3.0 its no longer possible. I get the following error
closure cannot implicitly captured a mutating self parameter
Here is a code snippet,
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
x += deltaX
y += deltaY
test { (a) -> Void in
// Get the Error in the below line.
self.x = Double(a)
}
}
mutating func test(myClosure: @escaping (_ a: Double) -> Void) {
myClosure(3)
}
}
I get that value types are not supposed to be mutable. I have cases, where I do have to modify one variable in the struct within one of the functions, when I receive the API response. (In the completion closure)
Is what I was doing in swift 2.2, impossible or is there way to accomplish this?