信号执行的操作

来源:互联网 发布:sql case when 多行 编辑:程序博客网 时间:2024/06/07 15:29
[signal subscribeNext:^(id x) {
        NSLog(@"%@",x);

    }];


MAP操作,就是对某信号原来的返回值,做一个函数处理,让该信号返回一个新值。

Map的意思就是映射.

    RACSignal *valid = [RACSignal combineLatest:@[_textView.rac_textSignal]

                                         reduce:^(NSString *feedBackString) {

                                             return @(feedBackString.length > 0);

                                         }];

    RAC(_feedButton, enabled) = valid;

    RAC(_feedButton, alpha) =[valid map:^(NSNumber *b) {

        return b.boolValue ? @1: @0.4;

    }];



filter操作就是过滤信号。

如果block返回yes,信号有效。否则无效。

    self.time = [NSDate date];

    //每隔1秒发一个信号

    RACSignal *repeatSignal = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] repeat];

    [repeatSignal subscribeNext:^(NSDate *time) {

        self.time = time;

    }];

    RACSignal *timeSignal = [self rac_valuesForKeyPath:@"time" observer:self];

    //为信号量添加过滤block

    [[timeSignal filter:^BOOL(NSDate* time) {

        NSDateComponents *com = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:time];

        return com.second % 2 == 0;

    }] subscribeNext:^(NSDate* time) {

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateFormat:@"HH:mm:ss"];

        self.label.text = [formatter stringFromDate:time];

    }];