基础-> 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的更多相关文章

  1. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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#? ...

  9. 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 ...

随机推荐

  1. The Best Blockchain Open Source Projects

    https://101blockchains.com/blockchain-open-source/ Blockchain has been the buzzword of the year for ...

  2. hdu1005-Number Sequence-(循环节)

    题意:已知f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7,给出A,B,n,求f(n) 题解:n巨大,循环肯定超时,在模7的 ...

  3. 【java异常】org.springframework.web.util.NestedServletException: Handler processing failed;Can't connect to X11 window server using 'localhost:10.0' as the value of th

    tomcat工程中创建二维码失败.抛出异常Can't connect to X11 window server using 'localhost:10.0' as the value of th 因为 ...

  4. chrome-解决该扩展程序未列在 Chrome 网上应用店中

    1.win10添加策略组 复制以下内容到.bat文件中,右键-以管理员身份运行,即可添加策略组 pushd "%~dp0" dir /b C:\Windows\servicing\ ...

  5. 写一个function,清除字符串前后的空格(兼容所有的浏览器)

    function trim1(str){ return str.replace(/(^\s*)|(\s*$)/g,""); }

  6. 2019 qbxt CSP-S考前冲刺班总结

    似乎--也没有太多好说的. ​ 但这是最后一次培训,因此还是应该写点什么的. ​ 记得状态最好的一次培训,是高一的第一次培训.那次是总共的第二次培训.第一次去的时候什么也不会,跟的非常吃力,每天都在疯 ...

  7. Centos开发小计

    1. 生成静态库,linux下库的规则是lib开头 g++ -c code.cpp ar cr libcode.a code.o

  8. asbDio (asbDio.dll)

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices ...

  9. mke2fs和mkfs命令使用

    1.mke2fs命令 在Linux系统下,mke2fs命令可用于创建磁盘分区上的”ext2/ext3”文件系统. (1)语法 mke2fs(选项)(参数) (2)常用选项 -b<区块大小> ...

  10. 红米note7几个问题处理

    1.听筒声音很小,外放正常,试了很多种方法,最终可行的是吧听筒网灰尘弄一下. 2.SAICLink车机互联:需要打开USB调试.USB安装.USB调试(安全设置)(不开启这个的话会连接后就断开).默认 ...