ProprtyType 的文本字段值与 Reactive Cocoa 4
Text field value to ProprtyType with Reactive Cocoa 4
我正在尝试将文本字段内容映射到 ProprtyType。我在视图模型中有 属性:
var property = MutableProperty<Int?> (nil)
我想把它绑定在viewDidLoad
let producer = textField.rac_textSignal().toSignalProducer().map { text in Int(text as! String) }
viewModel.property <~ producer
但我收到我不理解的错误:
Binary operator '<~' cannot be applied to operands of type 'MutableProperty' (aka 'MutableProperty>') and 'SignalProducer' (aka 'SignalProducer, NSError>')
我做错了什么?
因为绑定操作符<~要求信号产生者的错误类型必须是NoError
。所以使用 flatMapError:
捕获任何错误
let producer = textField.rac_textSignal().toSignalProducer()
.flatMapError { error in
return SignalProducer<AnyObject?, NoError>.empty
}
.map { text in Int(text as! String) }
property <~ producer
我正在尝试将文本字段内容映射到 ProprtyType。我在视图模型中有 属性:
var property = MutableProperty<Int?> (nil)
我想把它绑定在viewDidLoad
let producer = textField.rac_textSignal().toSignalProducer().map { text in Int(text as! String) }
viewModel.property <~ producer
但我收到我不理解的错误:
Binary operator '<~' cannot be applied to operands of type 'MutableProperty' (aka 'MutableProperty>') and 'SignalProducer' (aka 'SignalProducer, NSError>')
我做错了什么?
因为绑定操作符<~要求信号产生者的错误类型必须是NoError
。所以使用 flatMapError:
let producer = textField.rac_textSignal().toSignalProducer()
.flatMapError { error in
return SignalProducer<AnyObject?, NoError>.empty
}
.map { text in Int(text as! String) }
property <~ producer