博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RAC初步使用
阅读量:5058 次
发布时间:2019-06-12

本文共 4006 字,大约阅读时间需要 13 分钟。

信号基本流程:

  

//1:创建信号    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id
_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] 
{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]
{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]
{number = 3, name = (null)} 2018-11-21 18:53:24.319398+0800
{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 字典
( name, James ) 2018-11-21 18:53:24.319666+0800 key == name , value = James 2018-11-21 18:53:24.319899+0800
{number = 4, name = (null)} 2018-11-21 18:53:24.320207+0800 字典
( age, 18 ) 2018-11-21 18:53:24.320494+0800 key == age , value = 18 2018-11-21 18:53:24.342978+0800
{number = 4, name = (null)} 2018-11-21 18:53:24.343114+0800 字典
( gender, 1 ) 2018-11-21 18:53:24.343201+0800 key == gender , value = 1 */
View Code

 

转载于:https://www.cnblogs.com/jiefangzhe/p/9996965.html

你可能感兴趣的文章
控制文件的备份与恢复
查看>>
返回代码hdu 2054 A==B?
查看>>
Flink独立集群1
查看>>
iOS 8 地图
查看>>
20165235 第八周课下补做
查看>>
[leetcode] 1. Two Sum
查看>>
iOS 日常工作之常用宏定义大全
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
MVC Razor
查看>>
软件目录结构规范
查看>>
Windbg调试Sql Server 进程
查看>>
linux调度器系列
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>