Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created April 8, 2020 21:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/201a9d80ce558a16ecf142eb45bd2638 to your computer and use it in GitHub Desktop.
Save jasdev/201a9d80ce558a16ecf142eb45bd2638 to your computer and use it in GitHub Desktop.
`Publisher.assign(to:onWeak:)`.
import Combine
extension Publisher where Failure == Never {
func assign<Root: AnyObject>(
to keyPath: ReferenceWritableKeyPath<Root, Output>,
onWeak object: Root
) -> AnyCancellable {
sink { [weak object] value in
object?[keyPath: keyPath] = value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment