ReactiveObjC basic
基础-> https://www.jianshu.com/p/cd4031fbf8ff
- 在RAC中,万物皆信号。
- RAC 指的就是 RactiveCocoa ,是 Github 的一个开源框架,能够通过信号提供大量方便的事件处理方案,让我们更简单粗暴地去处理事件,现在分为 ReactiveObjC(OC) 和 ReactiveSwift(swift)。
- 团队协作时,必须注意一个点,对于很熟悉RAC的人来说,使用RAC是非常方便的。但对于不熟悉RAC的人来说,由于RAC的可阅读性是很差的,所以需耗费大量时间阅读和学习。
- 未避免循环引用,需使用@weakify(self),@strongify(self)。这两个宏至少是一对出现的
RAC架构框架图

信号流程

开始使用,要快
一、基本使用
1、基本控件
- UITextField
//监听文本输入
[[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}]; //可根据自己想要监听的事件选择
[[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"%@",x);
}];
//添加条件 -- 下面表示输入文字长度 > 10 时才会调用subscribeNext
[[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
return value.length > 10;
}] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"输入框内容:%@", x);
}];
UIButton
//监听按钮点击事件
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"-->%@",x);
}];
- 计时器(interval、delay)
//类似timer
@weakify(self)
self.disposable = [[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
@strongify(self)
NSLog(@"时间:%@", x); // x 是当前的时间
//关闭计时器
[self.disposable dispose];
}];
//延时
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"延时2秒"];
return nil;
}] delay:2] subscribeNext:^(id x) { NSLog(@"-->%@",x);
}];
2、监听属性变化
//监听self的name属性
[RACObserve(self, name) subscribeNext:^(id _Nullable x) {
NSLog(@"属性的改变-->%@",x);
}];
[[self rac_valuesForKeyPath:@"name" observer:self] subscribeNext:^(id _Nullable x) {
NSLog(@"属性的改变-->%@", x);
}];
//此处RAC宏相当于让_label订阅了_textField的文本变化信号
//赋值给label的text属性
RAC(_label, text) = _textField.rac_textSignal;
3、遍历数组和字典
//遍历数组
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
[array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"内容-->%@", x)
}];

4、监听 Notification 通知事件
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"-->%@", x);
}];
5、代替Delegate代理
//监听按钮点击方法的信号
//当执行完btnClickAction后会执行此订阅
[[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"-->%@", x);
}];
-(void) btnClickAction:(UIButton *)btn
{
NSLog(@"按钮点击");
}
二、RAC常用类
- RACSignal
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"
ReactiveObjC basic的更多相关文章
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
随机推荐
- 【oracle】ORA-00257 archiver error. Connect internal only, until freed
[原因]归档日志太多导致磁盘空间过小. [解决办法]删除日志或加大空间
- [教程]Ubuntu16.04安装QQ,Tim,微信,百度网盘等
[教程]Ubuntu16.04安装QQ,Tim,微信,百度网盘等 本文参考这篇blog step 1 先安装 deep-win环境. 戳这里下载压缩包 解压后在文件夹里打开终端,输入 sudo sh ...
- Pandas | 16 聚合
当有了滚动,扩展和ewm对象创建了以后,就有几种方法可以对数据执行聚合. DataFrame应用聚合 可以通过向整个DataFrame传递一个函数来进行聚合,或者通过标准的获取项目方法来选择一个列. ...
- docker自定义镜像上传阿里云
1.alpine制作jdk镜像 2.Alpine制作jre镜像(瘦身) 3.Docker镜像上传至阿里云 alpine制作jdk镜像 alpine Linux简介 1.Alpine Linux是一个轻 ...
- NOIP动态规划大合集
1.前言 NOIP2003-2014动态规划题目大合集,有简单的也有难的(对于我这种动态规划盲当然存在难的),今天就把这些东西归纳一下,做一个比较全面的总结,方便对动态规划有一个更深的理解. 2.NO ...
- 关于codeforces加载慢
昨天cdx报名cf,打开网页10多分钟才交了.... 今天问了wxy,百度了一下,以前也搜过,然后就忘记了. 今天记一下. 1.右键单击开始按钮,打开资源管理器,在资源管理器的地址栏中填写" ...
- C# byte数组与16进制间的相互转换
1.byte数组转16进制字符串 /// <summary> /// 将一个byte数组转换成16进制字符串 /// </summary> /// <param na ...
- javascript 检测浏览类型和版本
废话不多说了,直接就上代码吧,因为IE11以后的版本和之前的不一样了,所以有些关键字还需要注意.这里面判断IE的时候需要多注意.function getBrowserInfo(){ var ua = ...
- 本地VS调试服务器 IIS 程序
由于读书的关系,毕业后选择在武汉,工作三年,至今年5月份挪窝到沿海某二线城市,换城市相当于裸辞,一切从头开始,新的城市,新的居住地,新的空气,新工作,新挑战.一直忙忙碌碌,孜孜不倦的汲取着,担心脱队, ...
- 安装kafka + zookeeper集群
系统:centos 7.4 要求:jdk :1.8.x kafka_2.11-1.1.0 1.绑定/etc/hosts 10.10.10.xxx online-ops-xxx-0110.10 ...