Objective
A while ago looking at masonry, the new third-party layout framework, began to learn about chained programming. And then slowly began to understand functional programming and responsive programming. In this concentrated programming thought, began to contact and study the framework of Reactivecocoa. In the blogs of many great gods , have a high degree of evaluation of the framework, all kinds of show themselves how flexible use of Reactivecocoa. Today is mainly the framework of some of their own study notes.
1.ReactiveCocoa Introduction
Reactivecocoa (RAC), a new framework for iOS and OS development, is open source for GitHub.
The Reactivecocoa combines several programming styles:
Objective
A while ago looking at masonry, the new third-party layout framework, began to learn about chained programming. And then slowly began to understand functional programming and responsive programming. In this concentrated programming thought, began to contact and study the framework of Reactivecocoa. In the blogs of many great gods , have a high degree of evaluation of the framework, all kinds of show themselves how flexible use of Reactivecocoa. Today is mainly the framework of some of their own study notes.
1.ReactiveCocoa Introduction
Reactivecocoa (RAC), a new framework for iOS and OS development, is open source for GitHub.
The Reactivecocoa combines several programming styles:
Reactivecocoa is described by developers as a responsive functional programming framework (FRP); using RAC does not need to consider the order of calls, directly considering the results, making the code highly aggregated and easy to manage.
2.ReactiveCocoa use
2.1 racsiganl (Core class of RAC): Signal class for data transfer
Note the point:
The signal is a cold signal by default, the value changes, and does not trigger the change signal, only subscribe to the signal, will trigger the change signal.
How to subscribe to the signal: Subscribenext:
//Create a signal
racsignal *singal = [racsignal createsignal: ^racdisposable *(ID <RACSubscriber> subscriber) {
//Send a signal.
[subscriber sendnext:@ "Ricky"];
//If the data is not sent, it is best to send the signal to complete, the internal will automatically call [racdisposable disposable] unsubscribe signal.
[subscriber sendcompleted];
return [racdisposable disposablewithblock: ^{
//Signal cancellation
}];
}];
//subscribe signal, activate signal
[singal subscribenext: ^(ID x) {
//x: The signal is sent to the data
NSLog(@ "%@",x);
}];
2.2 Replacing Target and action
Uitextfield *textfield =[[uitextfield alloc]initwithframe: CGRectMake (+, +, + )];
TextField. BackgroundColor =[uicolor redcolor];
[self. View addsubview: TextField];
//Monitor input change of Uitextfield
[[TextField rac_textsignal] subscribenext: ^(ID x) {
NSLog(@ "%@",x);
}];
//x is the text of TextField, so change the ID to nsstring
[[TextField rac_textsignal] subscribenext:^ (NSString * text) {
//
NSLog (@ "%@", text);
// }];
UIButton *button =[UIButton buttonwithtype: Uibuttontypecustom];
button. frame = CGRectMake (+, +, + );
button. BackgroundColor = [uicolor yellowcolor];
[self. View addsubview: Button];
[button rac_signalforcontrolevents: UIControlEventTouchUpInside] subscribenext : ^(ID x) {
//This x is the button, already know the type can change the ID x to UIButton *button
NSLog(@ "%@",x);
}];
The RAC notification does not need to be removed, and the RAC handles the removal of the change notification itself
[[[nsnotificationcenter defaultcenter] rac_addobserverforname: Uiapplicationdidenterbackgroundnotification Object: nil] subscribenext: ^(ID x) {
NSLog(@ "%@",x);
}];
Delay a certain amount of time to do sth.
[[racscheduler mainthreadscheduler]afterdelay:2 Schedule: ^{
NSLog(@ "Ricky");
}];
How often each event is called
[ [racsignal interval:1 onscheduler:[racscheduler Mainthreadscheduler]] subscribenext: ^(ID x) {
NSLog(@ "%@",x);
}];
Uiscrollview *scroller =[[uiscrollview alloc]initwithframe: CGRectMake (+, +, + )];
[self. View addsubview: scroller];
[racobserve(scroller, contentoffset) subscribenext: ^( ID x) {
NSLog(@ "%@",x);
}];
IOS Development Reactivecocoa (Basic)