Skip to content

Instantly share code, notes, and snippets.

@dudarenko-io
Created November 22, 2017 14:19
Show Gist options
  • Save dudarenko-io/3c61d94010ef8d0160d03a1a567719a5 to your computer and use it in GitHub Desktop.
Save dudarenko-io/3c61d94010ef8d0160d03a1a567719a5 to your computer and use it in GitHub Desktop.
class MutableBox<T> {
var value: T
init(value: T) {
self.value = value
}
}
extension MutableBox: CustomDebugStringConvertible {
var debugDescription: String {
return String(reflecting: self.value)
}
}
class ObservableBox<T>: MutableBox<T> {
var valueChanged: (T) -> Void = { _ in }
override var value: T {
didSet {
self.valueChanged(oldValue)
}
}
}
let boxedArray = ObservableBox(value: [Int]())
boxedArray.valueChanged = { oldValue in
print("notification. oldValue: \(oldValue)")
}
print(boxedArray)
boxedArray.value.append(1)
print(boxedArray)
boxedArray.value.removeAll()
print(boxedArray)
// Output:
// []
// notification. oldValue: []
// [1]
// notification. oldValue: [1]
// []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment