美文网首页
RACChannelTo

RACChannelTo

作者: csp | 来源:发表于2017-06-24 09:53 被阅读204次

    <code>
    RACChannelTo(self, name) = RACChannelTo(self.model, name);
    RACChannelTo(self, blurb) = RACChannelTo(self.model, blurb);
    RACChannelTo(self, filmType, @(ASHRecipeFilmTypeColourNegative)) = RACChannelTo(self.model, filmType, @(ASHRecipeFilmTypeColourNegative));
    </code>
    这是一个双向绑定,self.name和self.model.name任何一个改变,另外一个也会改变。第三个参数是默认值,出现nil就用这个来代替。有了它,model和ViewModel就可以双向同步。

    <code>
    RACChannelTerminal *channelA = RACChannelTo(self, valueA);
    RACChannelTerminal *channelB = RACChannelTo(self, valueB);
    [[channelA map:^id(NSString value) {
    if ([value isEqualToString:@"西"]) {
    return @"东";
    }
    return value;
    }] subscribe:channelB];
    [[channelB map:^id(NSString value) {
    if ([value isEqualToString:@"左"]) {
    return @"右";
    }
    return value;
    }] subscribe:channelA];
    [[RACObserve(self, valueA) filter:^BOOL(id value) {
    return value ? YES : NO;
    }] subscribeNext:^(NSString
    x) {
    NSLog(@"你向%@", x);
    }];
    [[RACObserve(self, valueB) filter:^BOOL(id value) {
    return value ? YES : NO;
    }] subscribeNext:^(NSString
    x) {
    NSLog(@"他向%@", x);
    }];
    self.valueA = @"西";
    self.valueB = @"左";
    </code>

    相关文章

      网友评论

          本文标题:RACChannelTo

          本文链接:https://www.haomeiwen.com/subject/jiahcxtx.html