ReactiveCocoa(RAC)
好处:代码高聚合,方便我们管理;
链式编程:
CaculatorMaker.h
- #import <Foundation/Foundation.h>
- #define ADD
- #define KVO
- @interface CaculatorMaker : NSObject
- @property (nonatomic, assign) KVO int result;
- // +
- - (instancetype)add:(int)num; ADD
- - (CaculatorMaker *(^)(int num))add;
- // -
- // *
- - (CaculatorMaker *(^)(int num))multy;
- // /
- @end
CaculatorMaker.m
- #import "CaculatorMaker.h"
- @implementation CaculatorMaker
- //- (instancetype)add:(int)num
- //{
- // _result += num;
- // return self;
- //}
- - (CaculatorMaker *(^)(int))multy
- {
- return ^(int num){
- _result *= num;
- return self;
- };
- }
- - (CaculatorMaker * (^)(int num))add
- {
- return ^(int num){
- _result += num;
- return self;
- };
- }
- @end
NSObject+Caculator.h
- #import <Foundation/Foundation.h>
- #import "CaculatorMaker.h"
- @interface NSObject (Caculator)
- // 以后计算都使用这个方法,一调用这个方法就返回结果.
- + (int)makeCaculator:(void(^)(CaculatorMaker *))block;
- @end
NSObject+Caculator.m
- #import "NSObject+Caculator.h"
- #import "CaculatorMaker.h"
- @implementation NSObject (Caculator)
- + (int)makeCaculator:(void (^)(CaculatorMaker *))block
- {
- // 创建计算制造者
- CaculatorMaker *maker = [[CaculatorMaker alloc] init];
- // 计算
- block(maker);
- return maker.result;
- }
- @end
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- // add(1).add(2)
- // 1.创建计算制造者
- CaculatorMaker *maker = [[CaculatorMaker alloc] init];
- // 10 + 20 + 30 + 40
- // 链式编程思想: maker.add(10).add(20).add(30).add(40)
- // int result = [maker.add(10).add(20).add(30).add(40) result];
- // 提供一个没参数的add方法,返回值block
- // int reslut = [[[[[maker add:10] add:20] add:30] add:40] result];
- // block:使代码高聚合
- int result = [NSObject makeCaculator:^(CaculatorMaker *maker) {
- // 把所有的计算代码封装到这里
- maker.add().add();
- maker.add().add();
- maker.multy();
- }];
- NSLog(@"%d",result);
- }
// 之前开发中比较习惯,把事情封装到一个方法中,
// 链式编程思想:把要做的事情封装到block,给外界提供一个返回这个Block的方法
// 链式编程思想方法特点:方法返回值必须是block,block参数:放需要操作的内容,block返回值:方法调用者
响应式编程:(类似kvo)
c=a+b; a或者b改变 直接影响c的值;
函数式响应编程:
把一个操作写成一系列的方法;
ReactiveCocoa(RAC)的更多相关文章
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例
ReactiveCocoa是响应式编程(FRP)在iOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备
ReactiveCocoa是响应式编程(FRP)在IOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...
- iOS ReactiveCocoa(RAC)学习详解
概述: ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的一个框架,有时间,自己也了解学习了一下这个框架的一些基本的应用,其实你要学的话网上是有很多很多的博 ...
- ReactiveCocoa 和 MVVM 入门 (转)
翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...
- 【长篇高能】ReactiveCocoa 和 MVVM 入门
翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...
- 【转】伟大的RAC和MVVM入门(一)
原文:http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-introduction/ 翻译自ReactiveCocoa and MVV ...
- [iOS] 响应式编程开发-ReactiveCocoa(一)
什么是响应式编程 响应式编程是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流,而相关的计算模型会自动将变化的值通过数据流进行传播. 例如,在命令式编程环境中 ...
- MVVM设计模式加RAC响应式编程
一:为什么要用MVVM? 为什么要用MVVM?只是因为它不会让我时常懵逼. 每次做完项目过后,都会被自己庞大的ViewController代码吓坏,不管是什么网络请求.networking data ...
- 函数响应式编程(FRP)框架--ReactiveCocoa
由于工作原因,有段时间没更新博客了,甚是抱歉,只是,从今天開始我又活跃起来了,哈哈,于是决定每周更新一博.大家互相学习.交流. 今天呢.讨论一下关于ReactiveCocoa,这个採用函数响应式编程( ...
随机推荐
- IOS第13天(1,私人通讯录,登陆功能,界面的跳转传值,自定义cell,编辑界面)
******HMLoginViewController 登陆的界面 #import "HMLoginViewController.h" #import "MBProgre ...
- SSH相关
[root@www ~]# vim /etc/ssh/sshd_config# 1. 关于SSH Server 的整体设定,包含使用的port 啦,以及使用的密码演算方式# Port 22# SSH ...
- vba 工作案例1
手上有一份关于广东22个地市的数据,行列不符合预期的表结构,稍vba转换下,再text import 到oracle. Sub copy() ' ' copy 宏 ' ' 快捷键: Ctrl+Shif ...
- mysql开启慢查询
linux下: 一.在mysql中查询是否开启了慢查询mysql>SHOW VARIABLES LIKE '%slow%'; Variable_name Valuelog_slow_q ...
- Git: untrack a file in local repo only and keep it in the remote repo
You could update your index: git update-index --assume-unchanged nbproject/project.properties and ma ...
- 分时间uu
#include<stdio.h> int map[20][4]; typedef struct node{ int star; int end; }node; node dui[10 ...
- MyArrayAdapter 比较标准的写法
; i < mString.size(); i++) { insert(mString.get(i), i); } } notifyDataSetChanged(); LogUtils.LOGD ...
- toggle函数
$(function() { $('.love').toggle(function() { $(this).attr("src", "images/loved.png&q ...
- Nhiberate (二) 搭项目
使用: visual studio 2015 ;SQL SERVER 2012. 参考.测试可用 其中有点不太一样的地儿, ISession 的泛型方法: 用了 QueryOver<>,转 ...
- 产生0-9 A-Z a-z
>题目要求: >>产生26个大写字母 >>产生26个小写字母 >>产生0-9这10个阿拉伯数字 >程序实现: package cn.fury.test; ...