RAC的map值操作
2021-04-15 本文已影响0人
凯司机
值操作—map:
// 逐一操作单个信号值
// map遇到错误的时候会无一例外的返回这个错误,并立即结束信号。
RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@1];
[subscriber sendNext:@2];
// [subscriber sendError:[NSError new]];
[subscriber sendNext:@3];
[subscriber sendNext:@4];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"完成");
}];
}];
RACSignal *signalB = [signalA map:^id(NSNumber *value) {
return @(value.integerValue * 2);
}];
[[signalB subscribeNext:^(id x) {
NSLog(@"subscribeNext:%@",x);
}] dispose];
执行结果:
2021-04-14 14:38:17.763081+0800 xxxxx[3049:927533] subscribeNext:2
2021-04-14 14:38:17.763162+0800 xxxxx[3049:927533] subscribeNext:4
2021-04-14 14:38:17.763194+0800 xxxxx[3049:927533] subscribeNext:6
2021-04-14 14:38:17.763218+0800 xxxxx[3049:927533] subscribeNext:8
2021-04-14 14:38:17.764598+0800 xxxxx[3049:927533] 完成