响应式编程框架ReactiveCocoa介绍与入门

        ReactiveCocoa是Github团队开发的第三方函数式响应式编程框架,在目前市面上的很多iOS App都大量使用了这个框架。以下我简称这个框架为RAC.我下面会通过几篇博客来和大家一起学习这个强大的框架。该博客的案例代码已经上传至  https://github.com/chenyufeng1991/ReactiveCocoaDemo 。当然最好的学习方式是去阅读RAC的源码,Github上面RAC的官网地址  https://github.com/ReactiveCocoa/ReactiveCocoa 。在官网中,包含了源码,代码示例,文档。在本篇博客中,我主要是对官方文档进行翻译,并加入自己的理解与实现。这里实现的语言为OC。

 【1】ReactiveCocoa(RAC)介绍

        RAC是iOS的一个函数式响应式编程框架,而不是使用可变的变量去修改和替换原有的值。RAC提供了信号(RACSignal类)来监听当前和未来的值。通过信号的链接、组合和响应,可以让我们的代码持续的观察和更新值。我用一句话说就是:响应数据的变化。

       举个例子,我们可以绑定一个TextField输入框,只要绑定的值有改变,我们可以不添加任何额外的代码,就可以更新该输入框。工作原理类似于KVO,但是使用block块来替代重写“observeValueForKeyPath:ofObject:change:context”这个方法。信号也代表了异步操作,可以简化网络请求等异步代码。RAC的一个最主要优势就是提供了信号,统一处理了iOS中的异步行为,包括delegate,block回调,target-action机制,Notification和KVO。如下的例子:

// When self.username changes, logs the new name to the console.
//
// RACObserve(self, username) creates a new RACSignal that sends the current
// value of self.username, then the new value whenever it changes.
// -subscribeNext: will execute the block whenever the signal sends a value.
[RACObserve(self, username) subscribeNext:^(NSString *newName) {
    NSLog(@"%@", newName);
}];

当self.username的值改变时,log中就会输出新的值。RACObserve创建了一个新的RACSignal对象,可以发送最新的值到self.username,因此值就会随时改变。当信号signal发送新的值时,-subscribeNext就会执行block块中的代码。

      但是和KVO不一样,信号可以被链起来并操作,如下代码所示:

// Only logs names that starts with "j".
//
// -filter returns a new RACSignal that only sends a new value when its block
// returns YES.
[[RACObserve(self, username)
    filter:^(NSString *newName) {
        return [newName hasPrefix:@"j"];
    }]
    subscribeNext:^(NSString *newName) {
        NSLog(@"%@", newName);
    }];

上面的log中只会输出包含前缀为j的字符串。-filter会返回新的RACSignal对象,可以根据block返回新的值。

      信号同样可以用来得到状态,可以很方便的给属性一个信号和操作。如下代码所示:

// Creates a one-way binding so that self.createEnabled will be
// true whenever self.password and self.passwordConfirmation
// are equal.
//
// RAC() is a macro that makes the binding look nicer.
// 
// +combineLatest:reduce: takes an array of signals, executes the block with the
// latest value from each signal whenever any of them changes, and returns a new
// RACSignal that sends the return value of that block as values.
RAC(self, createEnabled) = [RACSignal 
    combineLatest:@[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ] 
    reduce:^(NSString *password, NSString *passwordConfirm) {
        return @([passwordConfirm isEqualToString:password]);
    }];

以上代码创建了一种新的数据绑定的方式,当self.password和self.passwordConfirmation相等的时候会返回true。RAC()是宏,可以让数据绑定看起来更加良好。+combineLatest:reduce: 是信号的数组,只要任意一个信号中的值有改变,就会用最新的值去执行block中的代码,然后返回新的RACSignal对象,用来发送新值。

      信号可以随时创建在任何值的流上,不同于KVO。举个例子,信号可以代表按钮点击:

// Logs a message whenever the button is pressed.
//
// RACCommand creates signals to represent UI actions. Each signal can
// represent a button press, for example, and have additional work associated
// with it.
//
// -rac_command is an addition to NSButton. The button will send itself on that
// command whenever it's pressed.
self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^(id _) {
    NSL
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值