Let's say I have very simple class:
class Box<T> {
var boxedObject:T
init(object: T) {
self.boxedObject = object
}
}
What I would like to achieve now is to add delegate, which can inform me that value in box has changed:
protocol BoxDelegate<T>: class {
func valueInBoxChanged(box: Box<T>) -> Void
}
class Box<T> {
var boxedObject: T {
didSet {
self.delegate?.valueInBoxChanged(self)
}
}
weak var delegate: BoxDelegate<T>?
init(object: T) {
self.boxedObject = object
}
}
This code is of course not working, because we don't have generic delegates. I can make delegate to be a struct with closure, but it's a bit ugly solution. How I should do such things in Swift?
