RAC映射有两种:map和flattenMap
//map
RACSubject *subject = [RACSubject subject];
RACSignal *bindSignal = [subject map:^id(id value) {
return [NSString stringWithFormat:@"prefix--%@----",value];
}];
[bindSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
[subject sendNext:@"zhangdanfeng"];
//常规
RACSubject *subject = [RACSubject subject];
RACSignal *bindSignal = [subject flattenMap:^RACStream *(id value) {
value = [NSString stringWithFormat:@"prefix--%@----",value];
return [RACReturnSignal return:value];
}];
[bindSignal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"zhangdanfeng"];
//flattenMap之信号中信号
/*最常规的拿出信号的信号中的值
RACSubject *subject = [RACSubject subject];
RACSubject *subjectOfSubject = [RACSubject subject];
[subjectOfSubject subscribeNext:^(RACSignal *x) {
[x subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
}];
[subjectOfSubject sendNext:subject];
[subject sendNext:@"zhangdanfeng"];
*/
/*使用switchToLatest简化一点
RACSubject *subject = [RACSubject subject];
RACSubject *subjectOfSubject = [RACSubject subject];
[subjectOfSubject.switchToLatest subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
[subjectOfSubject sendNext:subject];
[subject sendNext:@"zhangdanfeng"];
*/
//使用flattenMap
RACSubject *subject = [RACSubject subject];
RACSubject *subjectOfSubject = [RACSubject subject];
[[subjectOfSubject flattenMap:^RACStream *(id value) {
return value;
}] subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
[subjectOfSubject sendNext:subject];
[subject sendNext:@"zhangdanfeng"];
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。