【发布时间】:2016-09-26 12:34:54
【问题描述】:
在 swift 2.2 中,我们可以在闭包内改变结构或枚举,当它位于变异函数内时。但在 swift 3.0 中,它不再可能。我收到以下错误
闭包不能隐式捕获变异的自我参数
这是一个代码sn-p,
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)
}
}
我知道值类型不应该是可变的。在某些情况下,当我收到 API 响应时,我必须在其中一个函数内修改结构中的一个变量。 (在完成闭包中)
我在 swift 2.2 中所做的事情是不可能的,还是有办法做到这一点?
【问题讨论】:
-
删除
@escaping...你为什么有它? -
@MartinR 它正在逃逸,因为它的 Alamofire 完成关闭。
-
是的,我看过提案。看来已经不可能了。 @MartinR 那么我在这里使用课程的唯一可能方式是什么?