ReactiveCocoa:
首先使用Pod前,先查看一下版本信息,选择好需要的版本,特别是使用objc时候,不需要选择太新的,主要是已经不再更新了,在这里,我选择的是一个2.0版本,版本特性在大多数博文中都有详细介绍,我就不一一列举了。
PodFIle文件设置:
当然,只是简单的
也是可以的,我这样写无非是万无一失的,之后Pod update等待下载完成就可以。
打开工程文件夹中由pod新生成的workplace,这样就可以利用:
将RAC库导入到工程中去了。
在这之后,我们可以简单进行测试一下一些特性:
简单的信号订阅,这样在输入框中输入的内容就会被实时捕获:
当然你还可以进行对其他的特性进行简单测试:如信号合并等
在这里我实现了一个简单的登录界面demo:
1、当帐号密码不满足条件时输入框显示黄色,同时按钮无效
2、当帐号密码满足条件,输入框变为白色,按钮生效
我简单将一些实现的代码贴一下,方便大家试试:
//
// ViewController.m
// Test
//
// Created by 林之杰 on 15/11/18.
// Copyright © 2015年 林之杰. All rights reserved.
//
#import "ViewController.h"
#import "RWDummySignInService.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface ViewController ()
@property (strong,nonatomic) IBOutlet UITextField *newa;
@property (strong,nonatomic) IBOutlet UITextField *usrname;
@property (strong,nonatomic) IBOutlet UITextField *password;
@property (strong,nonatomic) IBOutlet UIButton *button;
@property (strong,nonatomic) RWDummySignInService *signInService;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.signInService = [RWDummySignInService new];
// [self.usrname.rac_textSignal subscribeNext:^(id text) {
// NSLog(@"%@",text);
// }];
//
//
//
RACSignal *signal = [self createSignal];
[signal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
// [[self.newa.rac_textSignal filter:^BOOL(NSString *text) {
// return text.length > 3;
// }] subscribeNext:^(id x) {
// NSLog(@"%@",x);
// }];
// [[[self.newa.rac_textSignal map:^id(NSString *text) {
// return @(text.length);
// }] filter:^BOOL(NSString *length) {
// return [length integerValue] >3;
// }] subscribeNext:^(id x) {
// NSLog(@"%@",x);
// }];
RACSignal * a = [self.usrname.rac_textSignal map:^id(NSString *text) {
return @([self isValidUsername:text]);
}];
RACSignal * b = [self.password.rac_textSignal map:^id(NSString *text) {
return @([self isValidPassword:text]);
}];
RAC(self.usrname,backgroundColor) =
[a map:^id(NSNumber *usernameValid) {
return [usernameValid boolValue]?[UIColor whiteColor]:[UIColor yellowColor];
}];
RAC(self.password,backgroundColor) =
[b map:^id(NSNumber *passwordValid) {
return [passwordValid boolValue]?[UIColor whiteColor]:[UIColor yellowColor];
}];
RACSignal *signUpActivateSignal =
[RACSignal combineLatest:@[a,b] reduce:^id(NSNumber *username,NSNumber *password){
return @([username boolValue] && [password boolValue]);
}];
[signUpActivateSignal subscribeNext:^(NSNumber *signupActive) {
self.button.enabled = [signupActive boolValue];
}];
[[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] flattenMap:^id(id value) {
return [self singInSignal];
}] subscribeNext:^(id x) {
NSLog(@"Sign in result:%@",x);
}];
// Do any additional setup after loading the view,typically from a nib.
}
- (BOOL)isValidUsername:(NSString *)username {
return username.length > 3;
}
- (BOOL)isValidPassword:(NSString *)password {
return password.length > 3;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (RACSignal*) createSignal{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"signal is created");
return nil;
}];
}
-(RACSignal *)singInSignal {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[self.signInService
signInWithUsername:self.usrname.text
password:self.password.text
complete:^(BOOL success) {
[subscriber sendNext:@(success)];
[subscriber sendCompleted];
}];
return nil;
}];
}
@end
好了,讲到这里,更多高级特性请移步以下链接学习:
• http://spin.atomicobject.com/2014/02/03/objective-c-delegate-pattern/
• http://blog.bignerdranch.com/4549-data-driven-ios-development-reactivecocoa/
• http://en.wikipedia.org/wiki/Functional_reactive_programming
• http://www.teehanlax.com/blog/reactivecocoa/
• http://www.teehanlax.com/blog/getting-started-with-reactivecocoa/
• http://nshipster.com/reactivecocoa/
• http://cocoasamurai.blogspot.com/2013/03/basic-mvvm-with-reactivecocoa.html
• http://iiiyu.com/2013/09/11/learning-ios-notes-twenty-eight/
• https://speakerdeck.com/andrewsardone/reactivecocoa-at-mobidevday-2013
• http://msdn.microsoft.com/en-us/library/hh848246.aspx
• http://www.itiger.me/?p=38
• http://blog.leezhong.com/ios/2013/12/27/reactivecocoa-2.html
• https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/Documentation/FrameworkOverview.md
• http://www.haskell.org/haskellwiki/Functional_Reactive_Programming
• http://blog.zhaojie.me/2009/09/functional-reactive-programming-for-csharp.html