A. Framework import

Cocoa Pods are recommended for easy management. (1) After installing the Pods, create a new Podfile in the project directory. The file content is 👇

platform:'ios'.'8.0'
target 'RWReactivePlayground' do
use_frameworks!
pod 'ReactiveCocoa'
end
Copy the code

(2) Set EMBEDDED_CONTENT_CONTAINS_SWIFT to YES in Build Setting

ReactiveCocoa has been imported successfully!!

Two. Simple use

In need to use the page introducing # import < > ReactiveCocoa/ReactiveCocoa. H

##### (1) rac_textSignal block This method is for the TextField listener, basically can omit the various input box delegate method. Ex. :

_textfild=[[UITextField alloc]initWithFrame:CGRectMake(20, 100, 100, 40)];
    _textfild.placeholder=@"Please enter";
    [self.view addSubview:_textfild];
[[_textfild rac_textSignal] subscribeNext:^(id x) {
        NSLog(@"% @",x);
    }];
Copy the code

The printed value of x is the input to the textField, and it will be called every time it is typed. And if this is the case, you only want to know if the password is correct until it is more than six characters long.

[[_textfild.rac_textSignal
filter:^BOOL(id value){
   NSString*text = value;
   return text.length > 6;
}]
subscribeNext:^(id x){
   NSLog(@"% @", x);
  }];
Copy the code

In another case, we only want to focus on the number of bytes entered by the user, but do not want to focus on his content. In this case, we can use the map block to change the data example of the event:

[[[_textfild.rac_textSignal
  map:^id(NSString*text){
    return @(text.length);
  }]
  filter:^BOOL(NSNumber*length){
    return[length integerValue] > 6;
  }]
  subscribeNext:^(id x){
    NSLog(@"% @", x);
  }];
Copy the code

At this point, you’ll notice that the input box will only output after the number of words exceeds six, and only the number of words. ##### (2) rac_signalForControlEvents listener method instead of addTarget

As mentioned above, this block can listen for click methods such as textField button. Ex. :

[[_textfild rac_signalForControlEvents:UIControlEventEditingChanged] 
   subscribeNext:^(id x) {
            NSLog(@"% @",x);
    }];

[[_button rac_signalForControlEvents:UIControlEventTouchUpInside]
   subscribeNext:^(id x) {
            NSLog(@"% @",x);
   }];
Copy the code

##### (3) rac_signalForSelector instead of a proxy event. Using this block requires passing in two parameters, the name of the method to execute, and the protocol to follow. For example, a main requires multiple AlertViews. Need to use tag to determine which alert is, which button is clicked, the code will be very tedious, in this case use RAC, see the effect 👇 example:

 UIAlertView * alertview=[[UIAlertView alloc]initWithTitle:@"RAC" message:@"RAC TE$XT" delegate:self cancelButtonTitle:@"queding" otherButtonTitles:@"quxaio", nil];
    [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
        NSLog(@"000000 = = = % @",tuple.first);
        NSLog(@"111111 = = = % @",tuple.second); }]; [alertview show]; // listen to which button is clicked [[alertView rac_buttonClickedSignal] subscribeNext:^(id x) {NSLog(@)"% @",x);
    }];
Copy the code

Click the alert button to find, click “OK” will output “00000”, click another same, the following block listen to click events, easy to solve!! ##### (4) ac_addObserverForName listening for notifications this is easier to use, register notifications normally write, when receiving notifications example:

[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postData" object:nil] subscribeNext:^(NSNotification *notification) {
        NSLog(@"% @",notification.name);
        NSLog(@"% @",notification.object);
    }];
Copy the code

Notification. name is the notification name and notification.object is the incoming value. ##### (5) RAC macros define listen object properties, instead of KVO KVO will not need to say more, listen, accept listen method, logout, the problem is dead, down to 👇 example:

// Listen for a property of an object and return a signal. [RACObserve(_tryla, text) subscribeNext:^(id x) { NSLog(@"% @",x); }]; // Used to bind a property of an object. RAC(_tryla,text) = _textField.rac_textSignal;Copy the code

RACObserve is a macro in RAC that takes two arguments, the object to listen on and the object property, to complete the listening. ##### (6) rac_sequence.signal convenience array and dictionary

NSArray *numbers = @[@1,@2,@3,@4]; [numbers.rac_sequence.signal subscribeNext:^(id x) { NSLog(@"% @",x); }]; // 2. Iterate through the dictionary, and the resulting key-value pairs are wrapped as RACTuple(tuple objects) NSDictionary *dict = @{@"name": @"xmg"The @"age": @}; [dict.rac_sequence.signal subscribeNext:^(RACTuple *x) {// RACTupleUnpack(NSString *key,NSString *value) = x; NSLog(@)"% @ % @",key,value);
    }];
Copy the code

That’s it, RAC is so powerful that I’ve only scratched the surface 😂😂😂