Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

ReactiveCocoa, RAC for short, is a functional responsive programming framework because it has the characteristics of both functional and responsive programming.

  • Because the framework programming ideas, it has a charm of function, it can realize the traditional design patterns, and event listeners can realize the function, such as KVO, notification, block the callback, the action, agreement, etc., it isn’t it the most superior characteristics of comprehensive, RAC most worth showing off, it provides a unified messaging system, This mechanism makes its code more concise, less code blocks of the same function, which is exactly in line with our programming ideas: high aggregation, low coupling, it is very suitable for the development of MVVM design pattern.
  • It’s not a complete replacement for traditional coding, however, and RAC still presents some headaches when it comes to multiplayer development and code maintenance.

Advantages: flexible and convenient to use, simple code, clear logic Disadvantages: high maintenance cost, difficult to trace the source of the problem

RAC’s unified messaging mechanism requires signals (SIGAL) for all actions.

  • 1). Signal creation, sending and receiving
// Create a cold signal, RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscribersubscriber) {// Send signal [subscriber SendNext :@"oh my God "]; Return [RACDisposable disposableWithBlock:^{NSLog(@" Signal completed ");}];}]; // Signal subscribeNext:^(id x) {NSLog(@"singalContent:%@", x);}];Copy the code
  • 2). The RAC ControlEventsThis method can easily implement the listening operation, and the logic behind itblock, and can also add gestures and listen.
[[self.textField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {  
    NSLog(@"%@", x);  
}];  

UITapGestureRecognizer *tap = [UITapGestureRecognizer new];  
[[tap rac_gestureSignal] subscribeNext:^(id x) {  
    NSLog(@"three:%@", x);  
}];  
[self.view addGestureRecognizer:tap];  
Copy the code
  • 3.) the RAC KVO
[[self.textField rac_valuesAndChangesForKeyPath:@"text" options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(id x) {  
    NSLog(@"%@", x);  
}];  
Copy the code
  • 4). Notice of RAC
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] SubscribeNext :^(id x) {NSLog(@" keyboard ");}];Copy the code
  • 5). RAC agreement
- (void)viewDidLoad { [super viewDidLoad]; Self.textfield. Delegate = self; [[self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)] SubscribeNext: ^ (id) x {NSLog (@ "click to print information: % @", x);}]; } - (void) textFieldDidBeginEditing: (UITextField at *) textField {NSLog (@ "began to edit"); }Copy the code
  • 6). RAC traverses groups and dictionaries

Equivalent to enumeration traversal, but more efficient

NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];  
[arr.rac_sequence.signal subscribeNext:^(id x) {  
    NSLog(@"arr : %@", x);  
}];  
NSDictionary *dic = @{@"name":@"yangBo", @"age":@"19"};  
[dic.rac_sequence.signal subscribeNext:^(id x) {  
    NSLog(@"dic : %@", x);  
}];  
Copy the code
  • 7). RAC signal processing (MAP, Filter, Combine)

① Do not process the signal

[[self.textField rac_textSignal] subscribeNext:^(id x) {  
    NSLog(@"doNothing:%@", x);  
}];  
Copy the code

② Filter the signal to determine whether to process the signal condition.

[[[self.textField rac_textSignal] filter:^BOOL(NSString* value) { if (value.length 3) { return YES; } return NO;  }] subscribeNext:^(id x) { NSLog(@"filter:%@", x); }];Copy the code

The first block returns the id type. If it returns “map now”, it is a signal conversion. The second block prints the value of your return, “map now”.

[[[self.textField rac_textSignal] map:^id(NSString* value) { if (value.length 3) { return @"map now"; } return value;  }] subscribeNext:^(id x) { NSLog(@"map:%@", x); }];Copy the code

(4) Signal combination

RACSignal *firstCombineSignal = [self.textField rac_textSignal]; RACSignal *secondeCombineSignal = [tap rac_gestureSignal]; RAC(self.label, backgroundColor) = [RACSignal combineLatest:@[firstCombineSignal, secondeCombineSignal] reduce:^id(NSString *text, UITapGestureRecognizer * tap) {/ / here signal logic judgment and processing the if (text. Length = = 3 && tap. State = = UIGestureRecognizerStateEnded) { return [UIColor redColor]; } return [UIColor cyanColor]; }];Copy the code

⑤ Signal correlation

RAC(self.label, text) = [self.textField rac_textSignal];
Copy the code