-
-
Save hslatman/235dce06c9b3d8a9b5ef3e7e7ae56431 to your computer and use it in GitHub Desktop.
KVO-driven model bindings
This file contains 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
extension NSObjectProtocol where Self: NSObject { | |
func observe<Value>(_ keyPath: KeyPath<Self, Value>, onChange: @escaping (Value) -> ()) -> NSKeyValueObservation { | |
return observe(keyPath, options: [.initial, .new]) { _, change in | |
// TODO: change.newValue should never be `nil`, but when observing an optional property that's set to `nil`, then change.newValue is `nil` instead of `Optional(nil)`. This is the bug report for this: https://bugs.swift.org/browse/SR-6066 | |
guard let newValue = change.newValue else { return } | |
onChange(newValue) | |
} | |
} | |
func bind<Value, Target>(_ sourceKeyPath: KeyPath<Self, Value>, to target: Target, at targetKeyPath: ReferenceWritableKeyPath<Target, Value>) -> NSKeyValueObservation { | |
return observe(sourceKeyPath) { target[keyPath: targetKeyPath] = $0 } | |
} | |
} | |
// ### Usage | |
class TestViewModel: NSObject { | |
@dynamic var title: String = "Test" | |
} | |
class TestViewController: UIViewController { | |
var observations: [NSKeyValueObservation] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
observations = [ | |
viewModel.bind(\.title, to: navigationItem, at: \.title) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment