Swift 2:结构上的变异方法不能从内部闭包中工作

时间:2015-09-22 17:30:36

标签: struct grand-central-dispatch swift2 mutating-function

我正试图在闭包内的一个结构上调用一个变异方法。它不起作用。

我正在安排从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)
    }

1 个答案:

答案 0 :(得分:1)

这似乎工作正常:

dispatch_after(time, highQ, { self.callChangeTimeStamp() })