I have a @Published string in my view model. I want to receive updates on its value in my view controller so that I can update the UI.
I am able to successfully get the updates through use of the sink subscriber. This works fine:
viewModel.$buttonText.sink { [weak self] buttonText in
self?.buttonOutlet.setTitle(buttonText, for: .normal)
}.store(in: &cancellables)
But I am looking for a one line approach. Something more like what you are able to do with UILabels using the assign subscriber:
viewModel.$title.assign(to: \.text, on: titleLabel).store(in: &cancellables)
I've tried accessing the buttonOutlet.titleLabel directly, but this of course doesn't work since we can't directly update the text (UIButton.titleLabel is read-only). And it also introduces the issue of unwrapping the titleLabel property:
viewModel.$buttonText.assign(to: \.!.text, on: buttonOutlet.titleLabel).store(in: &cancellables)
I don't know if I'm just struggling to find the correct syntax or if this is simply a limitation of Combine for the time being.