在swift 2.2中,当它在一个变异函数中时,我们可以在一个闭包中变异一个struct或enum.但在swift 3.0中它已不再可能.我收到以下错误
闭包不能隐式捕获变异的自参数
这是一段代码片段,
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的值类型不应该是可变的.我有一些情况,当我收到API响应时,我必须在其中一个函数中修改结构中的一个变量.(完成关闭)
我在swift 2.2中做的是什么,不可能或者有没有办法实现这个目标?