我开始使用 Reactive Cocoa,所以我写了一个小测试,看看我是否理解然后构造。目标是仅在发送某些信号后才将文本从文本字段输出到标签。我试过 rac_signalForControlEvents(UIControlEvents.TouchUpInside) 和非常简单的自定义信号。前者甚至不调用then闭包,而后者确实按预期工作。这里有什么问题?
func someSetupFunction()
{
#if true
// Why does this doesn't work?
button.rac_signalForControlEvents(UIControlEvents.TouchUpInside).then
{
let strongSelf = weakSelf
return strongSelf?.textField.rac_textSignal()
}.subscribeNext
{ object in
let strongSelf = weakSelf
strongSelf?.label.text = object as! String! + " - 0"
}
#else
// ... but this does?
customSignal().then
{
let strongSelf = weakSelf
return strongSelf?.textField.rac_textSignal()
}.subscribeNext
{ object in
let strongSelf = weakSelf
strongSelf?.label.text = object as! String! + " - 1"
}
#endif
}
func customSignal() -> RACSignal
{
return RACSignal.createSignal
{ subscriber in
subscriber.sendNext(nil)
subscriber.sendCompleted()
return nil
}
}