如何绑定两个RACChannelTerminals?

时间:2013-09-19 07:46:34

标签: objective-c macos binding reactive-programming reactive-cocoa

我有两个我希望同步的NSTextViews。我正在使用ReactiveCocoa从它们的可可绑定中生成RACChannelTerminals。

RACChannelTerminal *terminal1 = [textView1 rac_channelToBinding:@"attributedString" options:@{ NSContinuouslyUpdatesValueBindingOption: @(YES) }];
RACChannelTerminal *terminal2 = [textView2 rac_channelToBinding:@"attributedString" options:@{ NSContinuouslyUpdatesValueBindingOption: @(YES) }];

很自然地,我认为下一步就是制作RACChannel,然后插入两个终端。

RACChannel *channel = [RACChannel new];
channel.leadingTerminal = terminal1;
channel.followingTerminal = terminal2;

然后编译器说nope:Assignment to read only property。看起来这应该是直截了当的,所以我在这里做错了什么?如何使用自己的终端创建类似RACChannel的绑定?

1 个答案:

答案 0 :(得分:10)

两个RACChannelTerminal需要相互订阅。

[terminal1 subscribe:terminal2];
[terminal2 subscribe:terminal1];

初始值将被忽略,在输入新文本之前不会进行同步。

编辑:

为了让他们最初同步,我这样做了:

NSString *initialText = textView1.stringValue;

// ... setup channel terminals

[[terminal1 startWith:initialText] subscribe:terminal2];
[[terminal2 startWith:initialText] subscribe:terminal1];