是否将属性直接绑定到等同于订阅该信号然后设置属性的信号?

时间:2014-02-13 14:54:39

标签: objective-c reactive-cocoa

而不是在我的视图控制器中写这个:

@weakify(self);
[[self.view.emailAddressField.rac_textSignal distinctUntilChanged] subscribeNext:^(NSString *emailAddress) {
    @strongify(self);
    self.viewModel.emailAddress = emailAddress;
}];

使用以下内容会产生什么后果和影响?

RAC(self.viewModel, emailAddress) = [self.view.emailAddressField.rac_textSignal distinctUntilChanged];

2 个答案:

答案 0 :(得分:3)

它们是等价的,假设在调用时已经设置了self.viewModel

答案 1 :(得分:3)

请阅读源代码。

RAC(target, keyPath, nilValue) = signal;

类似于:

[signal setkeyPath:keyPath onObject:target nilValue:niValue];

setKeyPath:onObject:nilValue:的实现方式为:

RACDisposable *subscriptionDisposable = [self subscribeNext:^(id x) {
    NSObject *object = (__bridge id)objectPtr;
    [object setValue:x ?: nilValue forKeyPath:keyPath];
} error:^(NSError *error) {
    ...
} completed:^{
    ...
}];