The mutating
keyword allows a function on an enum to mutate itself, but is there a way to extend that ability to escaping closures?
I'm looking for a definition of the timer handler in start() below that will move MyTimer back to its .off state:
private enum MyTimer {
case off
case on(Date, Timer) // start time, timer
mutating func start() {
if case .on(_, let timer) = self {
timer.invalidate()
}
self = .on(Date(), Timer.scheduledTimer(withTimeInterval: maxComparisonInterval, repeats: false) { _ in
self.stop() // Error
})
}
mutating func stop() {
if case .on(_, let timer) = self {
timer.invalidate()
self = .off
}
}
}
The timer handler above causes a compiler error: "Escaping closure captures mutating 'self' parameter"