反应性 Cocoa - 然后是自定义信号与 UI 信号
Reactive Cocoa - then on custom signal vs UI signal
我开始使用 Reactive Cocoa 所以我写了一个小测试看看我是否理解 then 构造。目标是仅在发送某些信号后才将文本从文本字段输出到标签。我试过 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
}
}
来自 then 的文档:
Ignores all nexts from the receiver, waits for the receiver to complete,
then subscribes to a new signal.
button.rac_signalForControlEvents 没有完成(至少在点击按钮时没有完成),这就是什么都没有发生的原因。
iv-mexx 的原始答案可在 https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671
找到
我开始使用 Reactive Cocoa 所以我写了一个小测试看看我是否理解 then 构造。目标是仅在发送某些信号后才将文本从文本字段输出到标签。我试过 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
}
}
来自 then 的文档:
Ignores all nexts from the receiver, waits for the receiver to complete, then subscribes to a new signal.
button.rac_signalForControlEvents 没有完成(至少在点击按钮时没有完成),这就是什么都没有发生的原因。
iv-mexx 的原始答案可在 https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671
找到