函数响应式编程及ReactiveObjC学习笔记 (二)
之前我们初步认识了RAC的设计思路跟实现方式, 现在我们再来看看如果使用它以及它能帮我们做什么
One of the major advantages of RAC is that it provides a single, unified approach to dealing with asynchronous behaviors, including delegate methods, callback blocks, target-action mechanisms, notifications, and KVO.
官方是这样说的, RAC为我们提供了简单便捷实现代理 / block回调 / 事件 / 通知 / KVO的方式
我们先看RAC如何帮助我们快速实现KVO
首先我们新建一个Student类, 给它一个age的属性
#import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic, strong) NSString *age; @end
下面我们看一个简单的如何使用RAC来实现KVO
Student *stu = [[Student alloc] init];
// RAC KVO
[RACObserve(stu, age) subscribeNext:^(id _Nullable x) {
NSLog(@"stu的age改变为: %@", x);
}];
stu.age = @"";
运行看看:
-- ::19.704 RAC[:] stu的age改变为: (null)
-- ::19.704 RAC[:] stu的age改变为:
很方便对吧, 不用我们去add observe, 不用出来观察事件, 也不用我们去移除关注
不过大家注意到了没, 这里添加关注后block立即执行了一次, 大家可以依据实际项目情况加个条件判断做处理.
这里其实RAC还为我们提供了除了subscriber以外的操作, 后面再介绍给, 现在我们主要先来看RAC是怎么替我们做KVO的
我们再看看RAC如何帮我们实现target-action
我们创建一个项目, 在controller中添加一个button, 然后给button添加一个点击事件
如果是常规写法的话, 在创建完button后创建一个点击响应方法, 然后通过addTarget把响应方法跟button及事件绑定到一起
大概类似这样:
[button addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
- (void)btnAction {
NSLog(@"点击了按钮");
}
在上一篇我们提到过这样的劣势, 当代码比较多的时候结构容易乱, 维护的时候也不好查找方法
我们看看RAC如何帮我们优雅的实现
RAC为我们提供了rac_signalForControlEvents来处理UIControllerEvent, 我们试试看
[[button
rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"%@", x);
}];
因为不知道这里的x是什么, 我们直接打印看看结果
-- ::59.654 RAC[:] <UIButton: 0x7f95e0d069f0; frame = ( 350.5; ); opaque = NO; layer = <CALayer: 0x6080000269a0>>
当我们点击按钮打印了上面这些, 是我们创建的button对象
那么加入需要点击的时候给button更换背景图片或者标题就可以在这里处理了, 我们用改变颜色举例
[[button
rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(__kindof UIControl * _Nullable x) { x.backgroundColor = [UIColor redColor];
}];
运行后, 就可以看到如果点击按钮背景就会变成红色, 如果有点击事件也可以放在这里
但如果点击后要处理的逻辑比较多, 代码超过三行建议大家单独写一个方法供调用, 以免破坏代码的结构
RAC这样的使用方式, 让我的代码逻辑更加清晰紧凑了, 我们再看一个添加手势的例子
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap
rac_gestureSignal]
subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
NSLog(@"点击了屏幕");
NSLog(@"x: %@", x);
}];
[self.view addGestureRecognizer:tap];
运行看看:
-- ::59.246 RAC[:] 点击了屏幕
-- ::59.247 RAC[:] x: <UITapGestureRecognizer: 0x6000001a5160; state = Ended; view = <UIView 0x7fb932d03780>; target= <(action=sendNext:, target=<RACPassthroughSubscriber 0x60800003a920>)>>
这里x是一个手势, 我们可以直接拿来使用, 比如我们改变下添加手势这个view的颜色
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap
rac_gestureSignal]
subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
NSLog(@"点击了屏幕");
NSLog(@"x: %@", x);
x.view.backgroundColor = [UIColor redColor];
}];
[self.view addGestureRecognizer:tap];
这样手势的初始化, 方法等等都在一起, 让代码一目了然
接下来我们看看RAC如何帮我们实现通知的
我们常规的通知应该是这样, 在要接收通知的地方添加关注通知并写上通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAction) name:@"noti" object:nil];
- (void)notiAction {
NSLog(@"接到了通知");
}
然后在对应的地方发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:nil];
RAC会怎么帮我们实现呢?
[[[NSNotificationCenter defaultCenter]
rac_addObserverForName:@"noti" object:nil]
subscribeNext:^(NSNotification * _Nullable x) { NSLog(@"接到了通知");
}];
发送通知iOS已经很简单了, RAC没有做重复工作但帮我们把添加关注通知的方法改进了, 可以让事件和通知关注在一起
这样接口就清晰了
那么RAC如果帮我们实现代理呢?
我用UIAlertView给大家举个例子, 虽然苹果已经不推荐用这个 不过我们拿来当例子用用看
先写一个常规的AlertView
#import "ViewController.h"
#import <ReactiveObjC.h> @interface ViewController ()<UIAlertViewDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"RAC Delegate Test" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alertView show]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == ) { NSLog(@"点击了Cancel按钮");
} else { NSLog(@"点击了Ok按钮");
}
} @end
初始化alertView, 实现代理方法 这是我们常规的用法
那么我们再看看RAC如何做
#import "ViewController.h"
#import <ReactiveObjC.h> @interface ViewController ()<UIAlertViewDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"RAC Delegate Test" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [[self
rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)]
subscribeNext:^(RACTuple * _Nullable x) { NSLog(@"%@", x);
}]; [alertView show]; } @end
RAC为我们提供了一个rac_signalForSelector: fromProtoc方法帮我们实现代理
我们把x打印看看
-- ::07.138 RAC[:] <RACTuple: 0x6080000149f0> (
"<UIAlertView: 0x7fc7dfc0c620; frame = (0 0; 0 0); layer = <CALayer: 0x6000002218a0>>", )
我们看看这个RACTuple
@interface RACTuple : NSObject <NSCoding, NSCopying, NSFastEnumeration> @property (nonatomic, readonly) NSUInteger count; /// These properties all return the object at that index or nil if the number of
/// objects is less than the index.
@property (nonatomic, readonly, nullable) id first;
@property (nonatomic, readonly, nullable) id second;
@property (nonatomic, readonly, nullable) id third;
@property (nonatomic, readonly, nullable) id fourth;
@property (nonatomic, readonly, nullable) id fifth;
@property (nonatomic, readonly, nullable) id last;
打印看看
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"RAC Delegate Test" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[[self
rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)]
subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"%@", x);
NSLog(@"first: %@", x.first);
NSLog(@"second: %@", x.second);
NSLog(@"third: %@", x.third);
NSLog(@"fourth: %@", x.fourth);
NSLog(@"fifth: %@", x.fifth);
NSLog(@"last: %@", x.last);
}];
[alertView show];
结果为:
-- ::26.089 RAC[:] first: <UIAlertView: 0x7f814e604420; frame = ( ; ); layer = <CALayer: 0x60800003a3e0>>
-- ::26.090 RAC[:] second:
-- ::26.090 RAC[:] third: (null)
-- ::26.090 RAC[:] fourth: (null)
-- ::26.091 RAC[:] fifth: (null)
-- ::26.091 RAC[:] last:
第一个是alert本身, 第二个是index, 然后可以按我们的需要做处理了
另外要注意的是用RAC写代理是有局限的,它只能实现返回值为void的代理方法
先到这里, 现在我们知道我们能用RAC做什么了
下次我们继续看RAC的具体用法
函数响应式编程及ReactiveObjC学习笔记 (二)的更多相关文章
- 函数响应式编程及ReactiveObjC学习笔记 (-)
最近无意间看到一个视频讲的ReactiveObjC, 觉得挺好用的 但听完后只是了解个大概. 在网上找了些文章, 有的写的比较易懂但看完还是没觉得自己能比较好的使用RAC, 有的甚至让我看不下去 这两 ...
- 函数响应式编程及ReactiveObjC学习笔记 (三)
之前讲了RAC如何帮我们实现KVO / 代理 / 事件 / 通知 今天先不去分析它的核心代码, 我们先看看ReactiveObjC库里面一些特别的东西, 如果大家点开ReactiveObjC目录应该 ...
- 函数响应式编程及ReactiveObjC学习笔记 (四)
今天我们继续看其他的类别 UIImagePickerController+RACSignalSupport.h #import <UIKit/UIKit.h> @class RACDele ...
- 函数响应式编程(FRP)框架--ReactiveCocoa
由于工作原因,有段时间没更新博客了,甚是抱歉,只是,从今天開始我又活跃起来了,哈哈,于是决定每周更新一博.大家互相学习.交流. 今天呢.讨论一下关于ReactiveCocoa,这个採用函数响应式编程( ...
- 函数响应式编程(FRP)—基础概念篇
原文出处:http://ios.jobbole.com/86815/. 一函数响应式编程 说到函数响应式编程,就不得不提到函数式编程,他们俩有什么关系呢?今天我们就详细的解析一下他们的关系. 现在下面 ...
- RxJS入门之函数响应式编程
一.函数式编程 1.声明式(Declarativ) 和声明式相对应的编程⽅式叫做命令式编程(ImperativeProgramming),命令式编程也是最常见的⼀种编程⽅式. //命令式编程: fun ...
- 函数响应式编程(FRP)思想-Callback风格
序 ReactiveCocoa是IOS广为使用的技术框架,而ReactiveCocoa的核心思想就FRP.FRP不同于JAVA的object-oriented和AOP,FRP能让你的代码像数学一样简洁 ...
- ReactiveCocoa,最受欢迎的iOS函数响应式编程库(2.5版),没有之一!
简介 项目主页: ReactiveCocoa 实例下载: https://github.com/ios122/ios122 简评: 最受欢迎,最有价值的iOS响应式编程库,没有之一!iOS MVVM模 ...
- 函数响应式编程(FRP)从入门到”放弃”——基础概念篇
前言 研究ReactiveCocoa一段时间了,是时候总结一下学到的一些知识了. 一.函数响应式编程 说道函数响应式编程,就不得不提到函数式编程,它们俩到底有什么关系呢?今天我们就详细的解析一下他们的 ...
随机推荐
- MarkDown编辑器快捷方式
常用快捷: Ctrl+K 插入代码块 Ctrl+G 插入图片 Ctrl+B 文字加粗 Ctrl+I 文字倾斜 Ctrl+G 插入图片 Tab 默认新建高亮代码块 ">" 向右 ...
- 第二天0605下午——超链接<a>与图片<img>
今天下午学习了超链接<a>标签和图片<img>标签,以下面代码为例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- css块级元素居中
<!DOCTYPE html> <html> <head> <title>index</title> </head> <b ...
- solr学习笔记-开篇
由于工作需要,近期接触了一下全文检索的相关技术,从lucenen到solr开始慢慢一路摸爬滚打,仅以此文记录自己的学习里程和记录下各种坑. 本次学习基于以下环境版本: java7,solr5.5.4( ...
- CVE-2017-8464远程命令执行漏洞(震网漏洞)复现
前言 2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快捷方式时存在远程执行任意代码的高危漏洞,黑客可以通过U盘.网络共享等途径触发漏洞, ...
- java知识点整理
1 java 和Tomcat总结 脑图地址 (其中web 容器部分还需要继续完善,但是没找到相关文档) 跟着java Se 文档梳理了一下学习路线图(方便全面掌握要点,及时对自己查漏补缺),以及一些 ...
- JVM高级特性-一、java内存结构区域介绍
区域划分: java虚拟机在执行程序的过程中,将内存分为功能不同的几个区域,如下图: 此图列出了内存划分的各个区域,其中 线程私有的:程序计数器.虚拟机栈.本地方法栈 线程共享的:堆.方法区 下面,逐 ...
- 浅谈Cordova框架的一些理解
前言 因为工作原因,最近需要研究Cordova框架,看了其中的源码和实现方式,当场在看的时候马上能理解,但是事后再回去看相关源码时候却发现之前理解的内容又忘记了,又不得不重新开始看,所以总觉得需要记录 ...
- 基于Angularjs实现图片上传和下载
根据ng-file-uoload实现文件上传和下载实现 网上down下来ng-file-uoload.js,在项目中记得引入服务哦. 示例代码: FileUploaderCtrl.$inject = ...
- js中的confirm的运用
<a href="javascript:void(0);" onclick='tuichu_queren();'>安全退出</a> <script t ...