Skip to content

Instantly share code, notes, and snippets.

@gtokman
Created March 28, 2021 14:27
Show Gist options
  • Select an option

  • Save gtokman/f728b08d2469753086b5ff04d6e64d56 to your computer and use it in GitHub Desktop.

Select an option

Save gtokman/f728b08d2469753086b5ff04d6e64d56 to your computer and use it in GitHub Desktop.
assign - combine
// 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