I am trying to create a struct which contains a value along with an 'expiry time'. I want to use a struct rather than a class so it can be set as a @Published property in an Observable object. Here is the code:
struct MarineDatum {
var value: Double? {
didSet {
isNew = true
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false, block: { timer in self.makeOld()})
}
}
var isNew: Bool
var timer: Timer?
init(value: Double) {
self.value = value
self.isNew = true
}
mutating func makeOld() {
self.isNew = false
}
}
This code does exactly what I want, were it to actually compile, but I get an 'Escaping closure captures mutating 'self' parameter' error in the timer block.
Can anyone think of a way around this without creating an external variable to represent the timer object?