RAC的fileter和ignore
2021-04-19 本文已影响0人
凯司机
数量计算——filter:
// 过滤效果
// 通过判断条件来过滤符合条件的信号值
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"11"];
[subscriber sendNext:@"2222"];
[subscriber sendNext:@"3333"];
[subscriber sendNext:@"55"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"完成...");
}];
}] filter:^BOOL(NSString *value) {
return value.length > 2;
}] subscribeNext:^(id x) {
NSLog(@"subscribeNext:%@",x);
}] dispose];
// 结果:2222、3333
// 忽略某一项条件,取反
// 可以跟filter达到同等效果的ignore:
// 值不等于1的时候就返回这个值
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"11"];
[subscriber sendNext:@"2222"];
[subscriber sendNext:@"3333"];
[subscriber sendNext:@"55"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"完成...");
}];
}] ignore:@1] subscribeNext:^(id x) {
NSLog(@"subscribeNext:%@",x);
}] dispose];
2021-04-15 11:16:30.589031+0800 XXXXXX[3487:1041151] [DYMTLInitPlatform] platform initialization successful
2021-04-15 11:16:31.647022+0800 XXXXXX[3487:1041029] subscribeNext:11
2021-04-15 11:16:31.647104+0800 XXXXXX[3487:1041029] subscribeNext:2222
2021-04-15 11:16:31.647216+0800 XXXXXX[3487:1041029] subscribeNext:3333
2021-04-15 11:16:31.647236+0800 XXXXXX[3487:1041029] subscribeNext:55
2021-04-15 11:16:31.648315+0800 XXXXXX[3487:1041029] 完成...