Swift 2:结构上的变异方法在闭包内部不起作用
Swift 2: mutating method on struct not working from inside closure
我正在尝试从闭包中对我的一个结构调用变异方法。没用。
我正在安排从 dispatch_after
调用闭包。我收到错误消息 Partial application of 'mutating' method is not allowed
.
这在 Swift 1.2 中有效。 Not in Swift 2.更新后出现错误。这是一个独立示例,将在 Playground 中显示错误。
struct MutationTest {
var timestamp: Int = 0
mutating func changeTimestamp () {
timestamp += 1 //NO ERROR HERE
}
mutating func callChangeTimeStamp() {
changeTimestamp() //NO ERROR HERE
}
mutating func scheduleCallChangeTimestamp() {
let highQConstant = DISPATCH_QUEUE_PRIORITY_HIGH
let highQ = dispatch_get_global_queue(highQConstant, 0)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC))
dispatch_after(time, highQ, callChangeTimeStamp) //ERROR HERE
}
}
顺便说一句,如果我将 changeTimestamp
方法更改为:
,我会得到完全相同的错误
mutating func changeTimestamp () {
self = MutationTest(timestamp: timestamp + 1)
}
这似乎工作正常:
dispatch_after(time, highQ, { self.callChangeTimeStamp() })
我正在尝试从闭包中对我的一个结构调用变异方法。没用。
我正在安排从 dispatch_after
调用闭包。我收到错误消息 Partial application of 'mutating' method is not allowed
.
这在 Swift 1.2 中有效。 Not in Swift 2.更新后出现错误。这是一个独立示例,将在 Playground 中显示错误。
struct MutationTest {
var timestamp: Int = 0
mutating func changeTimestamp () {
timestamp += 1 //NO ERROR HERE
}
mutating func callChangeTimeStamp() {
changeTimestamp() //NO ERROR HERE
}
mutating func scheduleCallChangeTimestamp() {
let highQConstant = DISPATCH_QUEUE_PRIORITY_HIGH
let highQ = dispatch_get_global_queue(highQConstant, 0)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC))
dispatch_after(time, highQ, callChangeTimeStamp) //ERROR HERE
}
}
顺便说一句,如果我将 changeTimestamp
方法更改为:
mutating func changeTimestamp () {
self = MutationTest(timestamp: timestamp + 1)
}
这似乎工作正常:
dispatch_after(time, highQ, { self.callChangeTimeStamp() })