• RAC初步使用


    信号基本流程:

      

        //1:创建信号
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            //subscriber 对象不是一个对象
            //3:发送信号
            [subscriber sendNext:@"Cooci"];
            
            //请求网络 失败 error
            NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:10086 userInfo:@{@"key":@"10086错误"}];
            
            [subscriber sendError:error];
            //        [subscriber sendCompleted];
            
            //4.RACDisposable 销毁
            
            return [RACDisposable disposableWithBlock:^{
                
                NSLog(@"销毁了");
            }];
        }];
        
        
        //2:订阅信号
        [signal subscribeNext:^(id  _Nullable x) {
            
            NSLog(@"%@",x);
        }];
        
        //订阅错误信号
        
        [signal subscribeError:^(NSError * _Nullable error) {
            
            NSLog(@"%@",error);
        }];
        
    View Code

    一: KVO

        [RACObserve(self, name) subscribeNext:^(id  _Nullable x) {
            
            NSLog(@"%@",x);
            
        }];
    View Code

    二: 通知

        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            
            NSLog(@"%@",x);
        }];
    View Code

    三: 代理

        [[self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
            
        }];
    View Code

    四: target

        //Button
        [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            
        }];
        
        //UITextField
        [[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
            //x 为textField 中输入的text
        }];
        
        //UITapGestureRecognizer
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        self.label.userInteractionEnabled = YES;
        [self.label addGestureRecognizer:tap];
        
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            
        }];
    View Code

    五: timer

        [[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
            
            NSLog(@"%@",[NSThread currentThread]);
            
        }];
    View Code

    六: 结构

        //数组
        NSArray *array = @[@"Kobe",@"laker",@"34"];
        
        [array.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
            //子线程
            NSLog(@"%@",[NSThread currentThread]);
            NSLog(@"数组%@",x);
        }];
        
        /**
         2018-11-21 18:53:24.318270+0800 001---RAC初探[12854:18767540] <NSThread: 0x600002909200>{number = 3, name = (null)}
         2018-11-21 18:53:24.318529+0800 001---RAC初探[12854:18767540] 数组Kobe
         2018-11-21 18:53:24.319043+0800 001---RAC初探[12854:18767540] <NSThread: 0x600002909200>{number = 3, name = (null)}
         2018-11-21 18:53:24.319139+0800 001---RAC初探[12854:18767540] 数组laker
         2018-11-21 18:53:24.319340+0800 001---RAC初探[12854:18767540] <NSThread: 0x600002909200>{number = 3, name = (null)}
         2018-11-21 18:53:24.319398+0800  <NSThread: 0x600002909240>{number = 4, name = (null)}
         2018-11-21 18:53:24.319444+0800 001---RAC初探[12854:18767540] 数组34
    
         */
        
        NSDictionary *dict = @{@"name":@"James",@"age":@"18",@"gender":@"1"};
        
        [dict.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
            
            NSLog(@"%@",[NSThread currentThread]);
    
            //元祖
            NSLog(@"字典%@",x);
            
            RACTwoTuple *tuple = (RACTwoTuple *)x;
            
            NSLog(@"key == %@ , value = %@",tuple[0],tuple[1]);
            
        }];
        
        /**
         2018-11-21 18:53:24.319584+0800  字典<RACTwoTuple: 0x600003e7f0a0> (
         name,
         James
         )
         2018-11-21 18:53:24.319666+0800  key == name , value = James
         2018-11-21 18:53:24.319899+0800  <NSThread: 0x600002909240>{number = 4, name = (null)}
         2018-11-21 18:53:24.320207+0800  字典<RACTwoTuple: 0x600003e7f070> (
         age,
         18
         )
         2018-11-21 18:53:24.320494+0800  key == age , value = 18
         2018-11-21 18:53:24.342978+0800  <NSThread: 0x600002909240>{number = 4, name = (null)}
         2018-11-21 18:53:24.343114+0800  字典<RACTwoTuple: 0x600003e68950> (
         gender,
         1
         )
         2018-11-21 18:53:24.343201+0800  key == gender , value = 1
    
         */
    View Code
  • 相关阅读:
    备库归档重新传命令
    DG 参数详解
    ACTIVATE STANDBY
    Matplotlib模块:绘图和可视化
    Django总结一
    Django之用户认证—auth模块
    最长上升子序列
    Pandas模块:表计算与数据分析
    Django ORM操作

  • 原文地址:https://www.cnblogs.com/jiefangzhe/p/9996965.html
Copyright © 2020-2023  润新知