import UIKit
import Combine
class ViewController: UIViewController {
@IBOutlet weak var allowMessageSwitch: UISwitch!
@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var messageLabel: UILabel!
@Published var canSendMessages: Bool = false
@Published var newMsg: String = ""
private var switchSubscriber: AnyCancellable?
private var btnSubscriber: AnyCancellable?
override func viewDidLoad() {
allowMessageSwitch.isOn = false
super.viewDidLoad()
setupProcesscingChain()
}
func setupProcesscingChain() {
switchSubscriber = $canSendMessages.receive(on: DispatchQueue.main).assign(to: \.isEnabled, on: sendButton)
btnSubscriber = $newMsg.receive(on: DispatchQueue.main).assign(to: \.text, on: messageLabel)
}
@IBAction func didSwitch (_ sender: UISwitch) {
canSendMessages = sender.isOn
}
@IBAction func sendMessage( _ sender: Any) {
}
}
I am getting error in
btnSubscriber = $newMsg.receive(on: DispatchQueue.main).assign(to: \.text, on: messageLabel)
error msg is
Type of expression is ambiguous without more context
I dont understand why label does not work as Switcher (bool)
I assume it is because \.isEnabled
is not optional, and \.text
is optional..??
how can I make this work with the same format. this is for practice and to understand how Combine works.. please help!