我正试图在闭包内的一个结构上调用一个变异方法。它不起作用。
我正在安排从dispatch_after
调用闭包。我收到错误消息Partial application of 'mutating' method is not allowed
。
这在Swift 1.2中有效。不在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)
}
答案 0 :(得分:1)
这似乎工作正常:
dispatch_after(time, highQ, { self.callChangeTimeStamp() })