RAC操作方法之映射

RAC映射有两种:map和flattenMap
  • 1,map
    //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"];
  • 2,flattenMap
    //常规
    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"];


你可能感兴趣的:(RAC操作方法之映射)