Created
November 22, 2017 14:19
-
-
Save dudarenko-io/3c61d94010ef8d0160d03a1a567719a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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