ReactiveCocoa 用法实例
2016-07-11 10:24
411 查看
我个人非常推崇ReactiveCocoa,它就像中国的太极,太极生两仪,两仪生四象,四象生八卦,八卦生万物。ReactiveCocoa是一个高度抽象的编程框架,它真的很抽象,初看你不知道它是要干嘛的,等你用上了之后,就发现,有了它你是想干嘛就干嘛,编码从未如此流畅。
在此我不会讲ReactiveCocoa的原理,因为不能讲明白的才叫抽象。我也不会提及相关概念。我只是让你看看我用着它是有多爽。
你别动,你一动我就知道。
Objective-C
你唱歌,我就跳舞。
textField的内容长度隐射成BOOL值,绑定到confirmButton的enable属性上面,当textField输入内容不为空的时候,confirmButton的enable = YES。
Objective-C
你向西,他就向东,他向左,你就向右。
Objective-C
Objective-C
你是程序员,你帮我写个app吧。
Objective-C
Objective-C
Objective-C
知道你的频道,我就能听到你了。
Objective-C
Objective-C
生活是一个故事接一个故事。
Objective-C
Objective-C
污水都应该流入污水处理厂被处理。
Objective-C
Objective-C
你是红的,我是黄的,我们就是红黄的,你是白的,我没变,我们是白黄的。
Objective-C
Objective-C
你是红的,我是黄的,我们就是红黄的,你是白的,我没变,哦,那就等我变了再说吧。
Objective-C
Objective-C
我可以点石成金。
Objective-C
Objective-C
糖加水变成糖水。
Objective-C
Objective-C
未满十八岁,禁止进入。
Objective-C
Objective-C
打蛋液,煎鸡蛋,上盘。
Objective-C
Objective-C
把大象塞进冰箱只需要三步:打开冰箱门,把大象塞进冰箱,关上冰箱门。
Objective-C
Objective-C
我命令你马上投降。
Objective-C
Objective-C
等等我,我还有10秒钟就到了。
Objective-C
Objective-C
一次制作,多次观看。
Objective-C
Objective-C
每隔8个小时服一次药。
Objective-C
等了你一个小时了,你还没来,我走了。
Objective-C
Objective-C
成功之前可能需要数百次失败。
Objective-C
Objective-C
不好意思,这里一秒钟只能通过一个人。
Objective-C
Objective-C
直到世界的尽头才能把我们
Objective-C
Objective-C
转载自:http://ios.jobbole.com/82356/
在此我不会讲ReactiveCocoa的原理,因为不能讲明白的才叫抽象。我也不会提及相关概念。我只是让你看看我用着它是有多爽。
代码的四十八手
察值
你别动,你一动我就知道。
Objective-C
1 2 3 4 5 | @weakify(self); [RACObserve(self, value) subscribeNext:^(NSString* x) { @strongify(self); NSLog(@"你动了"); }]; |
单边
你唱歌,我就跳舞。
textField的内容长度隐射成BOOL值,绑定到confirmButton的enable属性上面,当textField输入内容不为空的时候,confirmButton的enable = YES。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 | RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"唱歌"]; [subscriber sendCompleted]; return nil; }]; RAC(self, value) = [signalA map:^id(NSString* value) { if ([value isEqualToString:@"唱歌"]) { return @"跳舞"; } return @""; }]; |
双边
你向西,他就向东,他向左,你就向右。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | RACChannelTerminal *channelA = RACChannelTo(self, valueA); RACChannelTerminal *channelB = RACChannelTo(self, valueB); [[channelA map:^id(NSString *value) { if ([value isEqualToString:@"西"]) { return @"东"; } return value; }] subscribe:channelB]; [[channelB map:^id(NSString *value) { if ([value isEqualToString:@"左"]) { return @"右"; } return value; }] subscribe:channelA]; [[RACObserve(self, valueA) filter:^BOOL(id value) { return value ? YES : NO; }] subscribeNext:^(NSString* x) { NSLog(@"你向%@", x); }]; [[RACObserve(self, valueB) filter:^BOOL(id value) { return value ? YES : NO; }] subscribeNext:^(NSString* x) { NSLog(@"他向%@", x); }]; self.valueA = @"西"; self.valueB = @"左"; |
1 2 3 4 | 2015-08-15 20:14:46.544 Test[2440:99901] 你向西 2015-08-15 20:14:46.544 Test[2440:99901] 他向东 2015-08-15 20:14:46.545 Test[2440:99901] 他向左 2015-08-15 20:14:46.545 Test[2440:99901] 你向右 |
代理
你是程序员,你帮我写个app吧。
Objective-C
1 2 3 | @protocol Programmer <NSObject> - (void)makeAnApp; @end |
1 2 3 4 5 6 7 | RACSignal *ProgrammerSignal = [self rac_signalForSelector:@selector(makeAnApp) fromProtocol:@protocol(Programmer)]; [ProgrammerSignal subscribeNext:^(RACTuple* x) { NSLog(@"花了一个月,app写好了"); }]; [self makeAnApp]; |
1 | 2015-08-15 20:46:45.720 Test[2817:114564] 花了一个月,app写好了 |
广播
知道你的频道,我就能听到你了。
Objective-C
1 2 3 4 | [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"代码之道频道" object:nil] subscribeNext:^(NSNotification* x) { NSLog(@"技巧:%@", x.userInfo[@"技巧"]); }]; [[NSNotificationCenter defaultCenter] postNotificationName:@"代码之道频道" object:nil userInfo:@{@"技巧":@"用心写"}]; |
1 | 2015-08-15 20:41:15.786 Test[2734:111505] 技巧:用心写 |
连接
生活是一个故事接一个故事。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 | RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"我恋爱啦"]; [subscriber sendCompleted]; return nil; }]; RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"我结婚啦"]; [subscriber sendCompleted]; return nil; }]; [[signalA concat:signalB] subscribeNext:^(id x) { NSLog(@"%@",x); }]; |
1 2 | 2015-08-15 12:19:46.707 Test[1845:64122] 我恋爱啦 2015-08-15 12:19:46.707 Test[1845:64122] 我结婚啦 |
合并
污水都应该流入污水处理厂被处理。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 | RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"纸厂污水"]; return nil; }]; RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"电镀厂污水"]; return nil; }]; [[RACSignal merge:@[signalA, signalB]] subscribeNext:^(id x) { NSLog(@"处理%@",x); }]; |
1 2 | 2015-08-15 12:10:05.371 Test[1770:60147] 处理纸厂污水 2015-08-15 12:10:05.372 Test[1770:60147] 处理电镀厂污水 |
组合
你是红的,我是黄的,我们就是红黄的,你是白的,我没变,我们是白黄的。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 | RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"红"]; [subscriber sendNext:@"白"]; return nil; }]; RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"白"]; return nil; }]; [[RACSignal combineLatest:@[signalA, signalB]] subscribeNext:^(RACTuple* x) { RACTupleUnpack(NSString *stringA, NSString *stringB) = x; NSLog(@"我们是%@%@的", stringA, stringB); }]; |
1 2 | 2015-08-15 12:14:19.837 Test[1808:62042] 我们就是红黄的 2015-08-15 12:14:19.837 Test[1808:62042] 我们是白黄的 |
压缩
你是红的,我是黄的,我们就是红黄的,你是白的,我没变,哦,那就等我变了再说吧。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 | RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"红"]; [subscriber sendNext:@"白"]; return nil; }]; RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"白"]; return nil; }]; [[signalA zipWith:signalB] subscribeNext:^(RACTuple* x) { RACTupleUnpack(NSString *stringA, NSString *stringB) = x; NSLog(@"我们是%@%@的", stringA, stringB); }]; |
1 | 2015-08-15 20:34:24.274 Test[2660:108483] 我们是红白的 |
映射
我可以点石成金。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 | RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"石"]; return nil; }] map:^id(NSString* value) { if ([value isEqualToString:@"石"]) { return @"金"; } return value; }]; [signal subscribeNext:^(id x) { NSLog(@"%@", x); }]; |
1 | 2015-08-16 20:00:12.853 Test[740:15871] 金 |
归约
糖加水变成糖水。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 | RACSignal *sugarSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"糖"]; return nil; }]; RACSignal *waterSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"水"]; return nil; }]; [[RACSignal combineLatest:@[sugarSignal, waterSignal] reduce:^id (NSString* sugar, NSString*water){ return [sugar stringByAppendingString:water]; }] subscribeNext:^(id x) { NSLog(@"%@", x); }]; |
1 | 2015-08-16 20:07:00.356 Test[807:19177] 糖水 |
过滤
未满十八岁,禁止进入。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 | [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@(15)]; [subscriber sendNext:@(17)]; [subscriber sendNext:@(21)]; [subscriber sendNext:@(14)]; [subscriber sendNext:@(30)]; return nil; }] filter:^BOOL(NSNumber* value) { return value.integerValue >= 18; }] subscribeNext:^(id x) { NSLog(@"%@", x); }]; |
1 2 | 2015-08-16 20:11:20.071 Test[860:21214] 21 2015-08-16 20:11:20.071 Test[860:21214] 30 |
扁平
打蛋液,煎鸡蛋,上盘。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"打蛋液"); [subscriber sendNext:@"蛋液"]; [subscriber sendCompleted]; return nil; }] flattenMap:^RACStream *(NSString* value) { return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"把%@倒进锅里面煎",value); [subscriber sendNext:@"煎蛋"]; [subscriber sendCompleted]; return nil; }]; }] flattenMap:^RACStream *(NSString* value) { return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"把%@装到盘里", value); [subscriber sendNext:@"上菜"]; [subscriber sendCompleted]; return nil; }]; }] subscribeNext:^(id x) { NSLog(@"%@", x); }]; |
1 2 3 4 | 2015-08-16 20:39:34.786 Test[1226:34386] 打蛋液 2015-08-16 20:39:34.787 Test[1226:34386] 把蛋液倒进锅里面煎 2015-08-16 20:39:34.787 Test[1226:34386] 把煎蛋装到盘里 2015-08-16 20:39:34.787 Test[1226:34386] 上菜 |
秩序
把大象塞进冰箱只需要三步:打开冰箱门,把大象塞进冰箱,关上冰箱门。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"打开冰箱门"); [subscriber sendCompleted]; return nil; }] then:^RACSignal *{ return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"把大象塞进冰箱"); [subscriber sendCompleted]; return nil; }]; }] then:^RACSignal *{ return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"关上冰箱门"); [subscriber sendCompleted]; return nil; }]; }] subscribeCompleted:^{ NSLog(@"把大象塞进冰箱了"); }]; |
1 2 3 4 | 2015-08-16 20:45:27.724 Test[1334:37870] 打开冰箱门 2015-08-16 20:45:27.725 Test[1334:37870] 把大象塞进冰箱 2015-08-16 20:45:27.725 Test[1334:37870] 关上冰箱门 2015-08-16 20:45:27.726 Test[1334:37870] 把大象塞进冰箱了 |
命令
我命令你马上投降。
Objective-C
1 2 3 4 5 6 7 8 | RACCommand *aCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) { return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"我投降了"); [subscriber sendCompleted]; return nil; }]; }]; [aCommand execute:nil]; |
1 | 2015-08-16 20:54:32.492 Test[1450:41849] 我投降了 |
延迟
等等我,我还有10秒钟就到了。
Objective-C
1 2 3 4 5 6 7 8 | [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"等等我,我还有10秒钟就到了"); [subscriber sendNext:nil]; [subscriber sendCompleted]; return nil; }] delay:10] subscribeNext:^(id x) { NSLog(@"我到了"); }]; |
1 2 | 2015-08-16 21:00:57.622 Test[1619:45924] 等等我,我还有10秒钟就到了 2015-08-16 21:01:07.624 Test[1619:45924] 我到了 |
重放
一次制作,多次观看。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 | RACSignal *replaySignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"大导演拍了一部电影《我的男票是程序员》"); [subscriber sendNext:@"《我的男票是程序员》"]; return nil; }] replay]; [replaySignal subscribeNext:^(id x) { NSLog(@"小明看了%@", x); }]; [replaySignal subscribeNext:^(id x) { NSLog(@"小红也看了%@", x); }]; |
1 2 3 | 2015-08-16 21:18:38.002 Test[1854:54712] 大导演拍了一部电影《我的男票是程序员》 2015-08-16 21:18:38.004 Test[1854:54712] 小明看了《我的男票是程序员》 2015-08-16 21:18:38.004 Test[1854:54712] 小红也看了《我的男票是程序员》 |
定时
每隔8个小时服一次药。
Objective-C
1 2 3 | [[RACSignal interval:60*60*8 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) { NSLog(@"吃药"); }]; |
超时
等了你一个小时了,你还没来,我走了。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"我快到了"); [subscriber sendNext:nil]; [subscriber sendCompleted]; return nil; }] delay:60*70] subscribeNext:^(id x) { [subscriber sendNext:nil]; [subscriber sendCompleted]; }]; return nil; }] timeout:60*60 onScheduler:[RACScheduler mainThreadScheduler]] subscribeError:^(NSError *error) { NSLog(@"等了你一个小时了,你还没来,我走了"); }]; |
1 2 | 2015-08-16 21:40:09.068 Test[2041:64720] 我快到了 2015-08-16 22:40:10.048 Test[2041:64720] 等了你一个小时了,你还没来,我走了 |
重试
成功之前可能需要数百次失败。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | __block int failedCount = 0; [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { if (failedCount < 100) { failedCount++; NSLog(@"我失败了"); [subscriber sendError:nil]; }else{ NSLog(@"经历了数百次失败后"); [subscriber sendNext:nil]; } return nil; }] retry] subscribeNext:^(id x) { NSLog(@"终于成功了"); }]; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 2015-08-16 21:59:07.159 Test[2411:77080] 我失败了 2015-08-16 21:59:07.159 Test[2411:77080] 我失败了 2015-08-16 21:59:07.159 Test[2411:77080] 我失败了 2015-08-16 21:59:07.159 Test[2411:77080] 我失败了 2015-08-16 21:59:07.160 Test[2411:77080] 我失败了 2015-08-16 21:59:07.160 Test[2411:77080] 我失败了 2015-08-16 21:59:07.161 Test[2411:77080] 我失败了 2015-08-16 21:59:07.162 Test[2411:77080] 我失败了 ... 2015-08-16 21:59:07.162 Test[2411:77080] 我失败了 2015-08-16 21:59:07.163 Test[2411:77080] 我失败了 2015-08-16 21:59:07.163 Test[2411:77080] 我失败了 2015-08-16 21:59:07.163 Test[2411:77080] 我失败了 2015-08-16 21:59:07.164 Test[2411:77080] 我失败了 2015-08-16 21:59:07.164 Test[2411:77080] 我失败了 2015-08-16 21:59:07.164 Test[2411:77080] 我失败了 2015-08-16 21:59:07.165 Test[2411:77080] 经历了数百次失败后 2015-08-16 21:59:07.165 Test[2411:77080] 终于成功了 |
节流
不好意思,这里一秒钟只能通过一个人。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"旅客A"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [subscriber sendNext:@"旅客B"]; }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [subscriber sendNext:@"旅客C"]; [subscriber sendNext:@"旅客D"]; [subscriber sendNext:@"旅客E"]; }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [subscriber sendNext:@"旅客F"]; }); return nil; }] throttle:1] subscribeNext:^(id x) { NSLog(@"%@通过了",x); }]; |
1 2 3 4 | 2015-08-16 22:08:45.677 Test[2618:83764] 旅客A 2015-08-16 22:08:46.737 Test[2618:83764] 旅客B 2015-08-16 22:08:47.822 Test[2618:83764] 旅客E 2015-08-16 22:08:48.920 Test[2618:83764] 旅客F |
条件
直到世界的尽头才能把我们
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) { [subscriber sendNext:@"直到世界的尽头才能把我们分开"]; }]; return nil; }] takeUntil:[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"世界的尽头到了"); [subscriber sendNext:@"世界的尽头到了"]; }); return nil; }]] subscribeNext:^(id x) { NSLog(@"%@", x); }]; |
1 2 3 4 5 6 | 2015-08-16 22:17:22.648 Test[2766:88737] 直到世界的尽头才能把我们分开 2015-08-16 22:17:23.648 Test[2766:88737] 直到世界的尽头才能把我们分开 2015-08-16 22:17:24.645 Test[2766:88737] 直到世界的尽头才能把我们分开 2015-08-16 22:17:25.648 Test[2766:88737] 直到世界的尽头才能把我们分开 2015-08-16 22:17:26.644 Test[2766:88737] 直到世界的尽头才能把我们分开 2015-08-16 22:17:26.645 Test[2766:88737] 世界的尽头到了 |
完事
ReactiveCocoa是如此优雅,一旦使用,根本停不下来,上面也只是它的一角冰山,但愿我能挑起你的兴趣。转载自:http://ios.jobbole.com/82356/
相关文章推荐
- ReactNative实现通知监听事件
- 【腾讯Bugly干货分享】React Native项目实战总结
- ES6环境搭建及react-router学习
- React Native之Redux使用详解之Actions(29)
- Web前端 - React 还是 AngularJS 持续更新中
- React Native常见报错
- 【JAVASCRIPT】React + Redux
- 【JAVASCRIPT】React 学习 - 登录实战
- ReactiveCocoa入门教程——第二部分
- 【ReactNative】react-native 布局
- React Native开发必备的10个插件包
- React Native开发必备的10个插件包
- React Native开发必备的10个插件包
- React独立组件间通信联动
- React Native UI布局总结
- reactiveCocoa rac_signalForControlEvents多次触发解决方法
- React在开发中的常用结构以及功能详解
- ReactiveCocoa的冷信号与热信号 探讨
- React升级后带来的两个小问题及处理
- React Native 常见问题总结(android端)