Skip to content

Instantly share code, notes, and snippets.

@amine2233
Forked from gdavis/assignNoRetain.swift
Created February 9, 2022 15:33
Show Gist options
  • Save amine2233/6345f8a1b9ad9e76c4b6cfab71eb2aa9 to your computer and use it in GitHub Desktop.
Save amine2233/6345f8a1b9ad9e76c4b6cfab71eb2aa9 to your computer and use it in GitHub Desktop.
Combine assign method without the memory leak
extension Publisher where Self.Failure == Never {
public func assignNoRetain<Root>(to keyPath: ReferenceWritableKeyPath<Root, Self.Output>, on object: Root) -> AnyCancellable where Root: AnyObject {
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