Simple Swift shared value binders
DSFValueBinders
ValueBinder
A ValueBinder creates a two-way binding object to allow sharing of a value between objects.
This is mildly similar to @Binding
in SwiftUI but doesn’t rely on SwiftUI – meaning it can be used pretty much anywhere Swift can.
Creating
You can define a binder using the standard initializer.
This initializer also allows you to supply a callback block that gets triggered when the wrappedValue
changes.
let countBinder = ValueBinder<Int>(0) { newValue in
Swift.print("countBinder changed: \(newValue)")
}
...
countBinder.wrappedValue = 4 // triggers the update callback
Registering for change updates
You can hand your ValueBinding object to another class which can supply a block to be called when the ValueBinder
wrapped value changes.
class AnotherClass {
init(_ binder: ValueBinder<Int>) {
binder.register(self) { newValue in
Swift.print("Binding detected change: \(newValue)")
}
}
}
Additionally, if you hold on to the binder object your class can update the ValueBinder value too!
class AnotherClass {
let countBinder: ValueBinder<Int>
init(_ binder: ValueBinder<Int>) {
countBinder = binder
countBinder.register(self) { newValue in
Swift.print("Binding detected change: \(newValue)")
}
}
func userPressed() {
countBinder.wrappedValue += 1
}
}
‘Registered’ listeners are bound weakly to the ValueBinder, meaning that a listener
Updating the ValueBinder value
Any object that holds a ValueBinder
object can update the wrapped value.
_countBinder.wrappedValue += 1
All objects that have registered for change callbacks will be notified of the change in value.
KeyPathBinder
A KeyPathBinder
is a specialization of the ValueBinder
that can track a dynamic keypath
// The dynamic property to bind to. This might be (for example) bound to a control from interface builder.
@objc dynamic var state: NSControl.State = .on
// Our binding object
lazy var boundKeyPath: KeyPathBinder<MyViewController, NSControl.StateValue> = {
return try! .init(self, keyPath: \.buttonState) { newValue in
Swift.print("boundKeyPath notifies change: \(String(describing: newValue))")
}
}()
Combine
Both binder types expose a property passthroughSubject
which you can hook up to your combine workflow.
let binder = ValueBinder(0)
...
let cancellable = binder.passthroughSubject.sink { newValue in
// do something with `newValue`
}
License
MIT License
Copyright (c) 2022 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.