diff --git a/SwiftTweaks/TweakLibrary.swift b/SwiftTweaks/TweakLibrary.swift index b391c9a..ae7cb44 100644 --- a/SwiftTweaks/TweakLibrary.swift +++ b/SwiftTweaks/TweakLibrary.swift @@ -29,6 +29,12 @@ public extension TweakLibraryType { self.defaultStore.unbind(identifier) } + /// Similar to `Combine.Subscribers.Assign`. returns nothing, since it doesn't strongly capture `on: OwnerObject`. example usage is: + /// `ExampleTweaks.bindWeakly(ExampleTweaks.colorTint, to: \.exampleButton.backgroundColor, on: self)` + static func bindWeakly(_ tweak: Tweak, to keypath: ReferenceWritableKeyPath, on owner: OwnerObject) { + self.defaultStore.bindWeakly(tweak, to: keypath, on: owner) + } + // Accepts a collection of Tweaks, and immediately calls the updateHandler. /// The updateHandler is then re-called each time any of the collection's tweaks change. /// Inside the updateHandler, you'll need to use `assign` to get the tweaks' current values. diff --git a/SwiftTweaks/TweakStore.swift b/SwiftTweaks/TweakStore.swift index 0dffaae..1458895 100644 --- a/SwiftTweaks/TweakStore.swift +++ b/SwiftTweaks/TweakStore.swift @@ -91,6 +91,14 @@ public final class TweakStore { tweakBindings[identifier.tweak] = existingTweakBindings.filter { $0.identifier != identifier } } + /// Similar to `Combine.Subscribers.Assign`. returns nothing, since it doesn't strongly capture `on: OwnerObject`. example usage is: + /// `ExampleTweaks.bindWeakly(ExampleTweaks.colorTint, to: \.exampleButton.backgroundColor, on: self)` + public func bindWeakly(_ tweak: Tweak, to keypath: ReferenceWritableKeyPath, on owner: OwnerObject) { + _ = self.bind(tweak) { [weak owner] tweakValue in + owner?[keyPath: keypath] = tweakValue + } + } + public func bindMultiple(_ tweaks: [TweakType], binding: @escaping () -> Void) -> MultiTweakBindingIdentifier { // Convert the array (which makes it easier to call a `bindTweakSet`) into a set (which makes it possible to cache the tweakSet) let tweakSet = Set(tweaks.map(AnyTweak.init))