当我得到什么选择:"Partial application of 'mutating' method is not allowed"

What alternative when I get: "Partial application of 'mutating' method is not allowed"

当我这样做时:

struct Test {
  var value: Int = 0

  mutating func increment(amount: Int) { value += amount }
  mutating func decrement(amount: Int) { value -= amount }

  func apply(technique: (Int) -> ()) {
    print("Before:\(value)")
    technique(2)
    print("After:\(value)")
  }

  mutating func main() {
    apply(technique: increment) //Error: "Partial application of 'mutating' method is not allowed"
  }

}

我收到此错误消息: 我读过这个: 并且可以看到问题,但无法确定替代代码应该是什么?

我的关键要求是我想设置一系列 "techniques",例如递增和递减,然后通过简单的调用 "apply" 它们。

感谢任何帮助:-)

一种方法是在 technique 中接受 inout Test。另外你应该使 apply mutating,因为它无疑会改变 Test.

的状态
mutating func apply(technique: (inout Test, Int) -> ()) {
    print("Before:\(value)")
    technique(&self, 2) // this is "kind of" analogous to "self.technique(2)"
    print("After:\(value)")
}

mutating func main() {
    apply(technique: {
        [=10=].increment(amount: )
    })
}