Created
March 28, 2021 14:27
-
-
Save gtokman/f728b08d2469753086b5ff04d6e64d56 to your computer and use it in GitHub Desktop.
assign - combine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new search subject with a default value | |
| class ViewController: UIViewController { | |
| @Published var buttonTapSubject: Int = 0 | |
| @Published var textSubject: String = "" | |
| var cancellables = Set<AnyCancellable>() | |
| let button = UIButton() | |
| let label = UILabel() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| /* layout code removed for brevity */ | |
| $buttonTapSubject | |
| .map{ String($0) } | |
| .assign(to: &$textSubject) // assign to another publisher | |
| $textSubject | |
| .compactMap { $0 } | |
| .assign(to: \.text, on: label) // assign to property on label | |
| .store(in: &cancellables) | |
| } | |
| @objc func didTap() { | |
| buttonTapSubject += 1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment