ReactiveCocoa (bottom), ReactiveCocoa (
1. Bind Method
The core method for ReactiveCocoa operations isbind
(Binding), and the core development method in RAC is alsoBind
, The previous development method isAssignment
With RAC development, we should focus on binding, that is, when creating an object, we can bind what we want to do in the future, instead of doing things after assigning values.
Column for example: the data is displayed on the control. Previously, the setModel method of the control is overwritten. With RAC, You can bind the data when creating the control.
The bind method is rarely used in development. bind is the underlying method in RAC. RAC has encapsulated many other useful methods. The underlying method is to call bind, which is easier to use than bind.
Method 1:
1 // listen to the content of the text box and splice the text "output:" 2 self. textField = [[UITextField alloc] initWithFrame: CGRectMake (20, 65,300,100)]; 3 self. textField. backgroundColor = [UIColor purpleColor]; 4 [self. view addSubview: self. textField]; 5 // concatenate 6 [self. textField. rac_textSignal subscribeNext: ^ (id x) {7 NSLog (@ "output: % @", x); 8}];
Input: Hello world in textField
Result:
Method 2: splice the returned results and use the bind method in RAC for processing.
A. bind method parameters: You need to input a block parameter whose return value is RACStreamBindBlock.
B. RACStreamBindBlock is a block type. The return value is a signal and the parameter (value, stop). Therefore, the block return value of the parameter is also a block.
1 // listen to the content of the text box and splice the text "output:" 2 self. textField = [[UITextField alloc] initWithFrame: CGRectMake (20, 65,300,100)]; 3 self. textField. backgroundColor = [UIColor purpleColor]; 4 [self. view addSubview: self. textField]; 5 [[self. textField. rac_textSignal bind: ^ RACSignalBindBlock _ Nonnull {6 7 // This block calls 8 return ^ RACSignal * (id value, BOOL * stop) as long as the source signal sends data) {9 // block Function: process the returned value. 10 // process the returned value and return the value through the signal. 11 // return signal. nil cannot be passed. If you really want to return an empty signal, use empty (actually an alloc init ). if empty is returned, the subscription chain is disconnected. return [RACSignal empty]; 12 return [RACReturnSignal return: [NSString stringWithFormat: @ "output: % @", value]; 13}; 14}] subscribeNext: ^ (id x) {15 NSLog (@ "subscribed location, that is, the signal processed: % @", x); 16}];
The output is as follows: