Search code examples
iosswiftuitextfieldreactive-cocoa

Text field value to ProprtyType with Reactive Cocoa 4


I'm trying to map text field content into ProprtyType. I have property in view model:

var property = MutableProperty<Int?> (nil)

And I wanted to bind it in viewDidLoad

let producer = textField.rac_textSignal().toSignalProducer().map { text in Int(text as! String) }
viewModel.property <~ producer

but I get error which I don't understand:

Binary operator '<~' cannot be applied to operands of type 'MutableProperty' (aka 'MutableProperty>') and 'SignalProducer' (aka 'SignalProducer, NSError>')

What do I do wrong?


Solution

  • Because the bind operator <~ requires the error type of the signal producer must be NoError. So catch any error using flatMapError:

    let producer = textField.rac_textSignal().toSignalProducer()
        .flatMapError { error in
            return SignalProducer<AnyObject?, NoError>.empty
        }
        .map { text in Int(text as! String) }
    
    property <~ producer