Skip to content

Instantly share code, notes, and snippets.

@gtokman
Created March 28, 2021 13:45
Show Gist options
  • Select an option

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

Select an option

Save gtokman/a43b8127f306b348bbfcd211206053ea to your computer and use it in GitHub Desktop.
Published - combine
// Create a new search subject with a default value
final class ViewController: UIViewController {
// Published subject initialized to zero
@Published var buttonTapSubject: Int = 0
var cancellables = Set<AnyCancellable>()
let button = UIButton()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
/* layout code removed for brevity */
// subscribe to button taps and assign value to label
$buttonTapSubject
.map{ String($0) }
.assign(to: \.text, 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