[self.usernameTextField.rac_textSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

打印结果

-- ::50.359 RWReactivePlayground[:a0b] i
-- ::50.436 RWReactivePlayground[:a0b] is
-- ::50.541 RWReactivePlayground[:a0b] is
-- ::50.695 RWReactivePlayground[:a0b] is t
-- ::50.831 RWReactivePlayground[:a0b] is th
-- ::50.878 RWReactivePlayground[:a0b] is thi
-- ::50.901 RWReactivePlayground[:a0b] is this
-- ::51.009 RWReactivePlayground[:a0b] is this
-- ::51.142 RWReactivePlayground[:a0b] is this m
-- ::51.236 RWReactivePlayground[:a0b] is this ma
-- ::51.335 RWReactivePlayground[:a0b] is this mag
-- ::51.439 RWReactivePlayground[:a0b] is this magi
-- ::51.535 RWReactivePlayground[:a0b] is this magic
-- ::51.774 RWReactivePlayground[:a0b] is this magic?
rac_textSignal文本信号订阅,传递给下一个,打印

文本信号过滤(长度过滤)
filter:
filter:^BOOL(id value) {
NSString *text = value;
return text.length > ;
}]
[[self.usernameTextField.rac_textSignal
filter:^BOOL(id value) {
NSString *text = value;
return text.length > ;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

打印结果

-- ::51.335 RWReactivePlayground[:a0b] is t
-- ::51.478 RWReactivePlayground[:a0b] is th
-- ::51.526 RWReactivePlayground[:a0b] is thi
-- ::51.548 RWReactivePlayground[:a0b] is this
-- ::51.676 RWReactivePlayground[:a0b] is this
-- ::51.798 RWReactivePlayground[:a0b] is this m
-- ::51.926 RWReactivePlayground[:a0b] is this ma
-- ::51.987 RWReactivePlayground[:a0b] is this mag
-- ::52.141 RWReactivePlayground[:a0b] is this magi
-- ::52.229 RWReactivePlayground[:a0b] is this magic
-- ::52.486 RWReactivePlayground[:a0b] is this magic?

可见大于三个的输入才会把输入的文本信号传递给订阅者

代码整理一下

RACSignal *usernameSourceSignal =
self.usernameTextField.rac_textSignal; RACSignal *filteredUsername = [usernameSourceSignal
filter:^BOOL(id value) {
NSString *text = value;
return text.length > ;
}]; [filteredUsername subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

订阅信号,过滤条件,传递给订阅者ReactiveCocoa初步-LMLPHP

[[self.usernameTextField.rac_textSignal
filter:^BOOL(id value) {
NSString *text = value; // implicit cast
return text.length > ;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

关于id value,在此例中就是传递的字符串

可直接修改替换

[[self.usernameTextField.rac_textSignal
filter:^BOOL(NSString *text) {
return text.length > ;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
rac_textSignal默认传递给订阅者的是文本内容,想传递其他的内容需要用到map
map:^id(NSString *text) {
return @(text.length);
}]
[[[self.usernameTextField.rac_textSignal
map:^id(NSString *text) {
return @(text.length);
}]
filter:^BOOL(NSNumber *length) {
return [length integerValue] > ;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

传递给订阅者文本长度信号,再对这个信号进行了文本长度大于3的过滤器,最近订阅者终于订阅到了这个实时的文本输入长度信号

-- ::54.566 RWReactivePlayground[:a0b]
-- ::54.725 RWReactivePlayground[:a0b]
-- ::54.853 RWReactivePlayground[:a0b]
-- ::55.061 RWReactivePlayground[:a0b]
-- ::55.197 RWReactivePlayground[:a0b]
-- ::55.300 RWReactivePlayground[:a0b]
-- ::55.462 RWReactivePlayground[:a0b]
-- ::55.558 RWReactivePlayground[:a0b]
-- ::55.646 RWReactivePlayground[:a0b]

ReactiveCocoa初步-LMLPHP

当我们需要用一个方法来判断输入的用户名或者密码符合要求不,一般会写个额外的方法,然后在这个方法里进行正则匹配。

那么 我们就要用这个方法来处理最初默认的文本内容信号

RACSignal *validUsernameSignal =
[self.usernameTextField.rac_textSignal
map:^id(NSString *text) {
return @([self isValidUsername:text]);
}]; RACSignal *validPasswordSignal =
[self.passwordTextField.rac_textSignal
map:^id(NSString *text) {
return @([self isValidPassword:text]);
}];

根据这个NSNumber来传递一个UIColor

[[validPasswordSignal
map:^id(NSNumber *passwordValid) {
return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
}]
subscribeNext:^(UIColor *color) {
self.passwordTextField.backgroundColor = color;
}];

看着上面的代码,还行,不过RAC有个更优雅的宏来展现

RAC(self.passwordTextField, backgroundColor) =
[validPasswordSignal
map:^id(NSNumber *passwordValid) {
return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
}]; RAC(self.usernameTextField, backgroundColor) =
[validUsernameSignal
map:^id(NSNumber *passwordValid) {
return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
}];

ReactiveCocoa初步-LMLPHP

通常情况下,登录一般是用户名和密码都匹配后才会在网络好的情况下登录成功。需要 进行信号混合

RACSignal *signUpActiveSignal =
[RACSignal combineLatest:@[validUsernameSignal, validPasswordSignal]
reduce:^id(NSNumber *usernameValid, NSNumber *passwordValid) {
return @([usernameValid boolValue] && [passwordValid boolValue]);
}];
05-11 09:23