好处:代码高聚合,方便我们管理;

链式编程:

CaculatorMaker.h

  1. #import <Foundation/Foundation.h>
  2. #define ADD
  3. #define KVO
  4. @interface CaculatorMaker : NSObject
  5. @property (nonatomic, assign) KVO int result;
  6. // +
  7. - (instancetype)add:(int)num; ADD
  8. - (CaculatorMaker *(^)(int num))add;
  9. // -
  10. // *
  11. - (CaculatorMaker *(^)(int num))multy;
  12. // /
  13. @end

CaculatorMaker.m

  1. #import "CaculatorMaker.h"
  2. @implementation CaculatorMaker
  3. //- (instancetype)add:(int)num
  4. //{
  5. // _result += num;
  6. // return self;
  7. //}
  8. - (CaculatorMaker *(^)(int))multy
  9. {
  10. return ^(int num){
  11. _result *= num;
  12. return self;
  13. };
  14. }
  15. - (CaculatorMaker * (^)(int num))add
  16. {
  17. return ^(int num){
  18. _result += num;
  19. return self;
  20. };
  21. }
  22. @end

NSObject+Caculator.h

  1. #import <Foundation/Foundation.h>
  2. #import "CaculatorMaker.h"
  3. @interface NSObject (Caculator)
  4. // 以后计算都使用这个方法,一调用这个方法就返回结果.
  5. + (int)makeCaculator:(void(^)(CaculatorMaker *))block;
  6. @end

NSObject+Caculator.m

  1. #import "NSObject+Caculator.h"
  2. #import "CaculatorMaker.h"
  3. @implementation NSObject (Caculator)
  4. + (int)makeCaculator:(void (^)(CaculatorMaker *))block
  5. {
  6. // 创建计算制造者
  7. CaculatorMaker *maker = [[CaculatorMaker alloc] init];
  8. // 计算
  9. block(maker);
  10. return maker.result;
  11. }
  12. @end
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4. // add(1).add(2)
  5. // 1.创建计算制造者
  6. CaculatorMaker *maker = [[CaculatorMaker alloc] init];
  7. // 10 + 20 + 30 + 40
  8. // 链式编程思想: maker.add(10).add(20).add(30).add(40)
  9. // int result = [maker.add(10).add(20).add(30).add(40) result];
  10. // 提供一个没参数的add方法,返回值block
  11. // int reslut = [[[[[maker add:10] add:20] add:30] add:40] result];
  12. // block:使代码高聚合
  13. int result = [NSObject makeCaculator:^(CaculatorMaker *maker) {
  14. // 把所有的计算代码封装到这里
  15. maker.add().add();
  16. maker.add().add();
  17. maker.multy();
  18. }];
  19. NSLog(@"%d",result);
  20. }

// 之前开发中比较习惯,把事情封装到一个方法中,

// 链式编程思想:把要做的事情封装到block,给外界提供一个返回这个Block的方法

// 链式编程思想方法特点:方法返回值必须是block,block参数:放需要操作的内容,block返回值:方法调用者

响应式编程:(类似kvo)

c=a+b;  a或者b改变  直接影响c的值;

函数式响应编程:

把一个操作写成一系列的方法;

ReactiveCocoa(RAC)的更多相关文章

  1. IOS响应式编程框架ReactiveCocoa(RAC)使用示例

    ReactiveCocoa是响应式编程(FRP)在iOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...

  2. IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备

    ReactiveCocoa是响应式编程(FRP)在IOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...

  3. iOS ReactiveCocoa(RAC)学习详解

    概述: ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的一个框架,有时间,自己也了解学习了一下这个框架的一些基本的应用,其实你要学的话网上是有很多很多的博 ...

  4. ReactiveCocoa 和 MVVM 入门 (转)

    翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...

  5. 【长篇高能】ReactiveCocoa 和 MVVM 入门

    翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...

  6. 【转】伟大的RAC和MVVM入门(一)

    原文:http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-introduction/   翻译自ReactiveCocoa and MVV ...

  7. [iOS] 响应式编程开发-ReactiveCocoa(一)

    什么是响应式编程 响应式编程是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流,而相关的计算模型会自动将变化的值通过数据流进行传播. 例如,在命令式编程环境中 ...

  8. MVVM设计模式加RAC响应式编程

    一:为什么要用MVVM? 为什么要用MVVM?只是因为它不会让我时常懵逼. 每次做完项目过后,都会被自己庞大的ViewController代码吓坏,不管是什么网络请求.networking data ...

  9. 函数响应式编程(FRP)框架--ReactiveCocoa

    由于工作原因,有段时间没更新博客了,甚是抱歉,只是,从今天開始我又活跃起来了,哈哈,于是决定每周更新一博.大家互相学习.交流. 今天呢.讨论一下关于ReactiveCocoa,这个採用函数响应式编程( ...

随机推荐

  1. IOS第13天(1,私人通讯录,登陆功能,界面的跳转传值,自定义cell,编辑界面)

    ******HMLoginViewController 登陆的界面 #import "HMLoginViewController.h" #import "MBProgre ...

  2. SSH相关

    [root@www ~]# vim /etc/ssh/sshd_config# 1. 关于SSH Server 的整体设定,包含使用的port 啦,以及使用的密码演算方式# Port 22# SSH ...

  3. vba 工作案例1

    手上有一份关于广东22个地市的数据,行列不符合预期的表结构,稍vba转换下,再text import 到oracle. Sub copy() ' ' copy 宏 ' ' 快捷键: Ctrl+Shif ...

  4. mysql开启慢查询

    linux下: 一.在mysql中查询是否开启了慢查询mysql>SHOW VARIABLES  LIKE '%slow%'; Variable_name     Valuelog_slow_q ...

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

  6. 分时间uu

    #include<stdio.h> int map[20][4]; typedef struct node{  int star;  int end; }node; node dui[10 ...

  7. MyArrayAdapter 比较标准的写法

    ; i < mString.size(); i++) { insert(mString.get(i), i); } } notifyDataSetChanged(); LogUtils.LOGD ...

  8. toggle函数

    $(function() { $('.love').toggle(function() { $(this).attr("src", "images/loved.png&q ...

  9. Nhiberate (二) 搭项目

    使用: visual studio 2015 ;SQL SERVER 2012. 参考.测试可用 其中有点不太一样的地儿, ISession 的泛型方法: 用了 QueryOver<>,转 ...

  10. 产生0-9 A-Z a-z

    >题目要求: >>产生26个大写字母 >>产生26个小写字母 >>产生0-9这10个阿拉伯数字 >程序实现: package cn.fury.test; ...