Created
March 10, 2019 16:22
-
-
Save ANSCoder/a9acc86635cf71876d398f8a4e049726 to your computer and use it in GitHub Desktop.
Boxing in Swift 4.2
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 Box<T>{ | |
typealias Listener = (T) -> Void | |
var listener: Listener? | |
var value: T { | |
didSet{ | |
listener?(value) | |
} | |
} | |
init(_ value: T) { | |
self.value = value | |
} | |
func bind(listener: Listener?){ | |
self.listener = listener | |
listener?(value) | |
} | |
} | |
//Example for Use | |
let boxInt: Box<String> = Box("") | |
boxInt.value = "Bob" | |
boxInt.bind { | |
print("value chanage \($0)") | |
} | |
boxInt.value = "John" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment