编程思想一响应式编程

前言

程序启动时在viewDidLoad中执行a=b+c,a=b+c表示将表达式的结果赋给a,而之后改变b或c的值不会影响a,除非在别的方法中进行刷新操作。但在响应式编程中,a的值会随着b或c的更新而更新。

使用场景

在MVC软件架构中,响应式编程允许将相关模型的变化自动反映到视图上,反之亦然。

典型框架

ReactiveCocoa  ReactiveCocoa介绍与使用

例子

创建ResponseToolClass类

#import typedef void(^responseBlock)(NSUInteger integerB , NSUInteger integerC);

@interface ResponseToolClass : NSObject

- (void)addIntegerB:(NSUInteger)integerB integerC:(NSUInteger)integerC;

- (void)results:(responseBlock)response;

@end

#import "ResponseToolClass.h"

@interface ResponseToolClass ()

@property (nonatomic , strong) void(^saveResponseBlock)(NSUInteger integerB , NSUInteger integerC);

@end

@implementation ResponseToolClass

- (void)addIntegerB:(NSUInteger)integerB integerC:(NSUInteger)integerC{

    self.saveResponseBlock(integerB, integerC);

}

- (void)results:(responseBlock)response{

    self -> _saveResponseBlock = [response copy];

}

@end

在ViewController中调用

@interface ViewController ()@property (nonatomic , strong) ResponseToolClass * toolClass;

@end

@implementation ViewController

- (void)viewDidLoad {

 [super viewDidLoad];

 __block NSUInteger integerA;

 [self.toolClass results:^(NSUInteger integerB, NSUInteger integerC) {

    integerA = integerB + integerC;       

     NSLog(@"%ld",integerA); 

 }];

};

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    NSUInteger integerB = arc4random() % 101;

    NSUInteger integerC = arc4random() % 10;

    [self.toolClass addIntegerB:integerB integerC:integerC];

}

- (ResponseToolClass *)toolClass{

    if (!_toolClass) {

        _toolClass = [[ResponseToolClass alloc] init];

    }

    return _toolClass;

}

此时点击屏幕,integerA随着点击,值在随着变化。即响应式编程思想

代码

文章中的代码都可以在我的GitHub ReactiveObjCDemo 找到

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,261评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,347评论 19 139
  • 城市的天空,停留在饱和度很低的蓝。 晚霞在天空燃烧,思念在风中散飘,我在十一月的北方。 一把吉他,随口开嗓,民谣的...
    胡垚阅读 4,326评论 8 10
  • 双弱当可成,单强亦能存。 若是两强者,必残伤一人。 注:这首打油诗的意思是,夫妻之间不要太强势。
    明哥明说阅读 773评论 0 0