ios开发 RAC中关键字用法

//
//  RacTestViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/5/21.
//

#import "RacTestViewController.h"
#import 
#import 
#import 



@interface RacTestViewController ()
@property(nonatomic,strong)UITextField *textField;
@end

@implementation RacTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   
    
    self.title = @"rac";
    self.view.backgroundColor = [UIColor whiteColor];
    [self _initTextfield];
    
  
    [self _distinctUntilChanged];
}

-(void)_initTextfield{
    
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
    self.textField.backgroundColor = [UIColor systemBlueColor];
    [self.view addSubview:self.textField];
    
    UIButton *barrierButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [barrierButton setTitle:@"修改textField的值" forState:(UIControlStateNormal)];
//    [barrierButton addTarget:self action:@selector(barrierAction:) forControlEvents:(UIControlEventTouchUpInside)];
    barrierButton.backgroundColor =[UIColor purpleColor];
    barrierButton.tag = 1001;
    [self.view addSubview:barrierButton];
    [barrierButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(-150);
    }];
    [[barrierButton rac_signalForControlEvents:(UIControlEventTouchUpInside)]subscribeNext:^(__kindof UIControl * _Nullable x) {
            
        self.textField.text = @"xingweixn";
    }];
    
}


//冷信号
-(void)_initSignal{
    
    //创建信号
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        
        //发送信号
        [subscriber sendNext:@"1"];
        [subscriber sendNext:@"2"];
        
        return [RACDisposable disposableWithBlock:^{
            //信号销毁
            NSLog(@"--信号销毁");
        }];
        
    }];
    
    //订阅信号
    [signal1 subscribeNext:^(id  _Nullable x) {
        NSLog(@"----传来的信号为%@",x);
    }];
    
    
}

//热信号
-(void)_initSubject{
    //创建信号
    RACSubject *subject = [RACSubject subject];
    
    //订阅信号
    [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"----传来的信号为%@",x);
    }];
    
    //发送信号
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
    [subject sendCompleted];
    
    
    
}

//bind是包装信号
-(void)_bind{
    //    //创建信号
       RACSubject *subject = [RACSubject subject];
      //绑定新的信号
      RACSignal *singnal=[subject bind:^RACSignalBindBlock _Nonnull {
          return ^RACSignal*(id value,BOOL *stop){
              NSLog(@"value---%@",value);
              NSLog(@"stop---%@",[NSNumber numberWithBool:stop]);
              
              return[RACReturnSignal return:value];
          };
      }];
      // 订阅信号
      [singnal subscribeNext:^(id  _Nullable x) {
          NSLog(@"bind x is [%@]",x);
      }];
      //发送信号
      [subject sendNext:@"send bind value 123"];

}

//bind是包装信号
-(void)_bind1{
    
    [[self.textField.rac_textSignal bind:^RACSignalBindBlock {
            return ^RACSignal*(id value,BOOL *stop){
                return [RACReturnSignal return:[NSString stringWithFormat:@"hello: %@",value]];
            };
    }] subscribeNext:^(id  _Nullable x) {
         
        //包装信号
        
        NSLog(@"包装后的输出为-----%@",x);
    }];
    
}


//concat 合并多个信号为1个信号,其中前一个信号必须sendCompleted后一个才会执行后一个
-(void)_concat{
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];
    RACSignal *signal = [sub1 concat:sub2];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出为-----%@",x);
    }];
    
    [sub1 sendNext:@"1"];
    [sub1 sendCompleted];
    [sub2 sendNext:@"2"];
   
    
}

//合并,与concat不同的是不需要前一个执行sendCompleted后边的才能执行,只要有一个信号来就执行合并操作,没有顺序
-(void)_merge{
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];
    RACSignal *signal = [sub1 merge:sub2];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出为-----%@",x);
    }];
    
    [sub1 sendNext:@"1"];
    [sub2 sendNext:@"2"];
    
}

//合并最新元素的数组会输出多次
-(void)_combineLatest{
    
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];
    RACSignal *signal = [RACSignal combineLatest:@[sub1,sub2]];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的元祖为--%@",x);
    }];
    [sub1 sendNext:@"a"];
    [sub2 sendNext:@"b"];
    [sub2 sendNext:@"B"];
    [sub1 sendNext:@"A"];
    
    /*
     2021-05-21 15:27:29.446570+0800 ObjcGoNew[10400:801952] 输出的元祖为-- (
         a,
         b
     )
     2021-05-21 15:27:29.446841+0800 ObjcGoNew[10400:801952] 输出的元祖为-- (
         a,
         B
     )
     2021-05-21 15:27:29.446947+0800 ObjcGoNew[10400:801952] 输出的元祖为-- (
         A,
         B
     )
     */
    
}


//reduce 用来处理元祖数据
-(void)reduce{
    
    
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];
//    RACSignal *signal = [RACSignal combineLatest:@[sub1,sub2] ];
    RACSignal *signal = [RACSignal combineLatest:@[sub1,sub2] reduce:^id _Nonnull(NSString *string1,NSString *string2){
        
        return [NSString stringWithFormat:@"%@-%@",string1,string2];
    }];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    [sub1 sendNext:@"a"];
    [sub2 sendNext:@"b"];
    [sub2 sendNext:@"B"];
    [sub1 sendNext:@"A"];
}


//flattenMap 底层使用bind实现
-(void)_flattenMap{
    
    
    RACSubject *sub1 = [RACSubject subject];
    RACSignal *subject2 = [sub1 flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return  [RACReturnSignal return:[NSString stringWithFormat:@"hello: %@",value]];
        
    }];
    
    [subject2 subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"输出的为--%@",x);
    }];
    
    [sub1 sendNext:@"123"];
    
}

//map 的底层实现是通过flattenMap 实现的。map 直接对数据进行处理,并且返回处理后的数据
-(void)_map{
    
    [[self.textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
            // 当源信号发出,就会调用这个block,修改源信号的内容
            // 返回值:就是处理完源信号的内容。
            return [NSString stringWithFormat:@"hello : %@",value];
        }] subscribeNext:^(id  _Nullable x) {
            NSLog(@"Map : %@",x); // hello: "x"
        }];
}

//过滤
-(void)_filter{
    RACSubject *subject = [RACSubject subject];
    
    RACSignal *signal = [subject filter:^BOOL(id  _Nullable value) {
        NSString *string = (NSString *)value;
        if (string.length > 10) {
            return YES;
        }else{
            return NO;
        }
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    
    [subject sendNext:@"112"];
    [subject sendNext:@"123456789011"];
}

//屏蔽
-(void)_ignore{
    
    RACSubject *subject = [RACSubject subject];
    
    RACSignal *signal = [subject ignore:@"ABC"];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    
    [subject sendNext:@"ABC"];
    [subject sendNext:@"ABC123"];
    [subject sendNext:@"123456789011"];
    
    /*
     2021-05-21 16:02:50.183235+0800 ObjcGoNew[10547:812550] 输出的为--ABC123
     2021-05-21 16:02:50.183488+0800 ObjcGoNew[10547:812550] 输出的为--123456789011
     */
}

//then忽略前一个发送的值,前一个必须sendCompleted,后后面的值才输出
-(void)_then{
    
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];

    RACSignal *signal = [sub1 then:^RACSignal * _Nonnull{
        return sub2;
    }];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    [sub1 sendNext:@"a"];
    [sub1 sendCompleted];
    [sub2 sendNext:@"b"];
    [sub2 sendNext:@"B"];
    
    /*
     2021-05-21 16:10:52.419816+0800 ObjcGoNew[10580:814837] 输出的为--b
     2021-05-21 16:10:52.420049+0800 ObjcGoNew[10580:814837] 输出的为--B
     */
   
    
}

//take 只要前几个信号 比如我只要1-4个
-(void)_take{
    
    
    RACSubject *sub1 = [RACSubject subject];
   
    //获取1-4次的信号, 输入0则不取信号
    RACSignal *signal = [sub1 take:4];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    [sub1 sendNext:@"1"];
    [sub1 sendNext:@"2"];
    [sub1 sendNext:@"3"];
    [sub1 sendNext:@"4"];
    [sub1 sendNext:@"5"];
    [sub1 sendNext:@"6"];
    [sub1 sendNext:@"7"];
    
    /*
     2021-05-21 16:16:06.600422+0800 ObjcGoNew[10600:816596] 输出的为--1
     2021-05-21 16:16:06.600766+0800 ObjcGoNew[10600:816596] 输出的为--2
     2021-05-21 16:16:06.600882+0800 ObjcGoNew[10600:816596] 输出的为--3
     2021-05-21 16:16:06.601014+0800 ObjcGoNew[10600:816596] 输出的为--4
     */
}

//skip跳过1-3的信号
-(void)_skip{
    
    RACSubject *sub1 = [RACSubject subject];
    RACSignal *signal = [sub1 skip:3];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    [sub1 sendNext:@"1"];
    [sub1 sendNext:@"2"];
    [sub1 sendNext:@"3"];
    [sub1 sendNext:@"4"];
    [sub1 sendNext:@"5"];
    [sub1 sendNext:@"6"];
    [sub1 sendNext:@"7"];
    
    /*
     2021-05-21 16:18:00.002219+0800 ObjcGoNew[10610:817558] 输出的为--4
     2021-05-21 16:18:00.002480+0800 ObjcGoNew[10610:817558] 输出的为--5
     2021-05-21 16:18:00.002588+0800 ObjcGoNew[10610:817558] 输出的为--6
     2021-05-21 16:18:00.002687+0800 ObjcGoNew[10610:817558] 输出的为--7
     */
}

//信号内的信号,获取信号内发送的最新信号,当发送多个内部信号的时候,只接受最新的内部信号
-(void)_switchToLatest{
    RACSubject *sub1 = [RACSubject subject];
    RACSubject *sub2 = [RACSubject subject];
    RACSubject *sub3 = [RACSubject subject];
    
    [sub1.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    
    [sub1 sendNext:sub3];
    [sub1 sendNext:sub2];
    [sub2 sendNext:@"1"];
    [sub2 sendNext:@"2"];
    [sub3 sendNext:@"3"];
    [sub3 sendNext:@"4"];
    [sub2 sendNext:@"5"];
    
    /*
     2021-05-21 16:27:23.053965+0800 ObjcGoNew[10651:820644] 输出的为--1
     2021-05-21 16:27:23.054141+0800 ObjcGoNew[10651:820644] 输出的为--2
     2021-05-21 16:27:23.054189+0800 ObjcGoNew[10651:820644] 输出的为--5
     */
    
}


//忽略重复的信号
-(void)_distinctUntilChanged{
    
    RACSubject *sub1 = [RACSubject subject];
    [sub1.distinctUntilChanged subscribeNext:^(id  _Nullable x) {
        NSLog(@"输出的为--%@",x);
    }];
    
    [sub1 sendNext:@"1"];
    [sub1 sendNext:@"1"];
    [sub1 sendNext:@"1"];
    [sub1 sendNext:@"2"];
    
    
    /*
     2021-05-21 16:31:39.072997+0800 ObjcGoNew[10667:821706] 输出的为--1
     2021-05-21 16:31:39.073593+0800 ObjcGoNew[10667:821706] 输出的为--2
     */
    
}

@end

你可能感兴趣的:(ios开发 RAC中关键字用法)