Skip to content

Commit 0f12d08

Browse files
committed
TweakStore: add new bindWeakly(:to:on:) helper
1 parent 3dec92a commit 0f12d08

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

SwiftTweaks/TweakStore.swift

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public final class TweakStore {
9191
tweakBindings[identifier.tweak] = existingTweakBindings.filter { $0.identifier != identifier }
9292
}
9393

94+
/// Similar to `Combine.Subscribers.Assign`. returns nothing, since it doesn't strongly capture `on: OwnerObject`. example usage is:
95+
/// `ExampleTweaks.bindWeakly(ExampleTweaks.colorTint, to: \.exampleButton.backgroundColor, on: self)`
96+
public func bindWeakly<T, OwnerObject: AnyObject>(_ tweak: Tweak<T>, to keypath: ReferenceWritableKeyPath<OwnerObject, T>, on owner: OwnerObject) {
97+
_ = self.bind(tweak) { [weak owner] tweakValue in
98+
owner?[keyPath: keypath] = tweakValue
99+
}
100+
}
101+
94102
public func bindMultiple(_ tweaks: [TweakType], binding: @escaping () -> Void) -> MultiTweakBindingIdentifier {
95103
// Convert the array (which makes it easier to call a `bindTweakSet`) into a set (which makes it possible to cache the tweakSet)
96104
let tweakSet = Set(tweaks.map(AnyTweak.init))

0 commit comments

Comments
 (0)