ReactiveCocoa初步
生活随笔
收集整理的這篇文章主要介紹了
ReactiveCocoa初步
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[self.usernameTextField.rac_textSignal subscribeNext:^(id x) {NSLog(@"%@", x);
}];
文本信號過濾(長度過濾)
filter: filter:^BOOL(id value) {NSString *text = value;return text.length > 3;}] [[self.usernameTextField.rac_textSignalfilter:^BOOL(id value) {NSString *text = value;return text.length > 3;}]subscribeNext:^(id x) {NSLog(@"%@", x);}];
打印結果
2013-12-24 14:48:50.359 RWReactivePlayground[9193:a0b] i 2013-12-24 14:48:50.436 RWReactivePlayground[9193:a0b] is 2013-12-24 14:48:50.541 RWReactivePlayground[9193:a0b] is 2013-12-24 14:48:50.695 RWReactivePlayground[9193:a0b] is t 2013-12-24 14:48:50.831 RWReactivePlayground[9193:a0b] is th 2013-12-24 14:48:50.878 RWReactivePlayground[9193:a0b] is thi 2013-12-24 14:48:50.901 RWReactivePlayground[9193:a0b] is this 2013-12-24 14:48:51.009 RWReactivePlayground[9193:a0b] is this 2013-12-24 14:48:51.142 RWReactivePlayground[9193:a0b] is this m 2013-12-24 14:48:51.236 RWReactivePlayground[9193:a0b] is this ma 2013-12-24 14:48:51.335 RWReactivePlayground[9193:a0b] is this mag 2013-12-24 14:48:51.439 RWReactivePlayground[9193:a0b] is this magi 2013-12-24 14:48:51.535 RWReactivePlayground[9193:a0b] is this magic 2013-12-24 14:48:51.774 RWReactivePlayground[9193:a0b] is this magic? rac_textSignal文本信號訂閱,傳遞給下一個,打印文本信號過濾(長度過濾)
filter: filter:^BOOL(id value) {NSString *text = value;return text.length > 3;}] [[self.usernameTextField.rac_textSignalfilter:^BOOL(id value) {NSString *text = value;return text.length > 3;}]subscribeNext:^(id x) {NSLog(@"%@", x);}];
打印結果
2013-12-26 08:17:51.335 RWReactivePlayground[9654:a0b] is t 2013-12-26 08:17:51.478 RWReactivePlayground[9654:a0b] is th 2013-12-26 08:17:51.526 RWReactivePlayground[9654:a0b] is thi 2013-12-26 08:17:51.548 RWReactivePlayground[9654:a0b] is this 2013-12-26 08:17:51.676 RWReactivePlayground[9654:a0b] is this 2013-12-26 08:17:51.798 RWReactivePlayground[9654:a0b] is this m 2013-12-26 08:17:51.926 RWReactivePlayground[9654:a0b] is this ma 2013-12-26 08:17:51.987 RWReactivePlayground[9654:a0b] is this mag 2013-12-26 08:17:52.141 RWReactivePlayground[9654:a0b] is this magi 2013-12-26 08:17:52.229 RWReactivePlayground[9654:a0b] is this magic 2013-12-26 08:17:52.486 RWReactivePlayground[9654:a0b] is this magic?可見大于三個的輸入才會把輸入的文本信號傳遞給訂閱者
代碼整理一下
RACSignal *usernameSourceSignal = self.usernameTextField.rac_textSignal;RACSignal *filteredUsername = [usernameSourceSignal filter:^BOOL(id value) {NSString *text = value;return text.length > 3;}];[filteredUsername subscribeNext:^(id x) {NSLog(@"%@", x); }];訂閱信號,過濾條件,傳遞給訂閱者
?
?
[[self.usernameTextField.rac_textSignalfilter:^BOOL(id value) {NSString *text = value; // implicit castreturn text.length > 3;}]subscribeNext:^(id x) {NSLog(@"%@", x);}];關于id value,在此例中就是傳遞的字符串
可直接修改替換
[[self.usernameTextField.rac_textSignalfilter:^BOOL(NSString *text) {return text.length > 3;}]subscribeNext:^(id x) {NSLog(@"%@", x);}];?
rac_textSignal默認傳遞給訂閱者的是文本內容,想傳遞其他的內容需要用到map map:^id(NSString *text) {return @(text.length);}] [[[self.usernameTextField.rac_textSignalmap:^id(NSString *text) {return @(text.length);}]filter:^BOOL(NSNumber *length) {return [length integerValue] > 3;}]subscribeNext:^(id x) {NSLog(@"%@", x);}];傳遞給訂閱者文本長度信號,再對這個信號進行了文本長度大于3的過濾器,最近訂閱者終于訂閱到了這個實時的文本輸入長度信號
2013-12-26 12:06:54.566 RWReactivePlayground[10079:a0b] 4 2013-12-26 12:06:54.725 RWReactivePlayground[10079:a0b] 5 2013-12-26 12:06:54.853 RWReactivePlayground[10079:a0b] 6 2013-12-26 12:06:55.061 RWReactivePlayground[10079:a0b] 7 2013-12-26 12:06:55.197 RWReactivePlayground[10079:a0b] 8 2013-12-26 12:06:55.300 RWReactivePlayground[10079:a0b] 9 2013-12-26 12:06:55.462 RWReactivePlayground[10079:a0b] 10 2013-12-26 12:06:55.558 RWReactivePlayground[10079:a0b] 11 2013-12-26 12:06:55.646 RWReactivePlayground[10079:a0b] 12?
當我們需要用一個方法來判斷輸入的用戶名或者密碼符合要求不,一般會寫個額外的方法,然后在這個方法里進行正則匹配。
那么 我們就要用這個方法來處理最初默認的文本內容信號
RACSignal *validUsernameSignal =[self.usernameTextField.rac_textSignalmap:^id(NSString *text) {return @([self isValidUsername:text]);}];RACSignal *validPasswordSignal =[self.passwordTextField.rac_textSignalmap:^id(NSString *text) {return @([self isValidPassword:text]);}];?
根據這個NSNumber來傳遞一個UIColor
[[validPasswordSignalmap:^id(NSNumber *passwordValid) {return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];}]subscribeNext:^(UIColor *color) {self.passwordTextField.backgroundColor = color;}];看著上面的代碼,還行,不過RAC有個更優雅的宏來展現
RAC(self.passwordTextField, backgroundColor) =[validPasswordSignalmap:^id(NSNumber *passwordValid) {return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];}];RAC(self.usernameTextField, backgroundColor) =[validUsernameSignalmap:^id(NSNumber *passwordValid) {return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];}];?
通常情況下,登錄一般是用戶名和密碼都匹配后才會在網絡好的情況下登錄成功。需要 進行信號混合
RACSignal *signUpActiveSignal =[RACSignal combineLatest:@[validUsernameSignal, validPasswordSignal]reduce:^id(NSNumber *usernameValid, NSNumber *passwordValid) {return @([usernameValid boolValue] && [passwordValid boolValue]);}];?
轉載于:https://www.cnblogs.com/songxing10000/p/4933543.html
總結
以上是生活随笔為你收集整理的ReactiveCocoa初步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 探寻阿里云服务器迈入2.0时代的技术要点
- 下一篇: html中嵌入天气预报