Source: Yangcs (Yangcs)
Brief introduction
This article mainly reviews the --ios functional programming && responsive programming concept , how to realize the process of functional programming step-by-step, to read the Masonry && SnapKit source code has certain help.
Image matching
As an iOS developer, you must have used masnory/snapkit;
- Masonry is a third-party framework that is well-suited for automatic layout in OC development;
- Snapkit is a swift version of the automatic layout framework created by the masonry team;
If you have not used the automatic layout in the original or native Apple version of the VFL language, then I have to give you a "praise"!
A typical masonry statement:
| 1 |
Make. CenterY. Equalto(self. View). Offset(+); |
That's the "." Let the masonry more vivid show to the developer, in my heart
Masnory/snapkit functional programming of a classic masterpiece, very worthy of reference and learning!
The benefits of chained programming do not have to say, used Masnory/snapkit know;
So the question is, how to write chained programming?
One-step implementation of a functional programming
- New Project Command +shift + N
- Create a Person class
and add two methods to it
| 12 |
- (void)run; - (void)study; |
Simple implementation:
| 123456 |
- (void)run{ NSLog(@"Run"); } - (void)study { NSLog(@"study") } |
- Instantiate and invoke the associated method
| 123 |
person*person = [[personalloc]init]; [personrun]; [personstudy]; |
The above three steps to achieve a very simple program;
Final goal
| 12 |
Person . Runblock(). Studyblock(). Runblock(); Person . Studyblock(). Studyblock(). Runblock(); |
Decomposition target
1. Implement this method first to call
| 1 |
[[person run1] study1]; |
2. Achieve the ultimate goal again
Obviously, if you want to implement [person RUN1] to invoke a method, then run1 needs an object to return, allowing the object to invoke Study1.
After this analysis, it is simple to add a return value.
Code
Declaration of the method
| 12 |
-(person*)run1; -(person*)study1; |
Implementation of the method
| 12345678 |
- (person*)run1 { NSLog(@"Run"); return [[personalloc] init]; }- (person*)study1{ NSLog(@"study"); return[[personalloc]init]; } |
Achieve the ultimate goal:
| 1 |
person. Runblock(). Studyblock(). Runblock(); |
In OC, the ' () ' block is () executed in the form of a, assuming that if a block is returned, then I can use it () to achieve the effect of runblock ().
Combined with our decomposition steps, Runblock () represents a block, and if the return value of the block is an object, then another method is called, so you can always link it down! Achieve the goal we want!
Code:
Statement
| 12 |
< Span class= "Crayon-o" >- (person* (^ () ) runBlock< Span class= "Crayon-sy", - (Person* (^ () ) studyBlock< Span class= "Crayon-sy", |
Realize:
| 1234567891011121314 |
- (person* (^)())Runblock { Person* (^block ( = ^ ( { NSLog(@"Run"); return self ; }; return block; } - (person* (^)())Studyblock { Person* (^block ( = ^ ( { NSLog(@"study"); return self ; }; return block; } |
Let's take a look at a functional programming of masonry.
| 1 |
Make. Right. Equalto(self. Right). Insets(kpadding); |
The function that is finally called:
Returned is a block with a return value of Masconstraint.
| 12345 |
- (masconstraint* (^)(ID))Equalto { return ^ID(idattribute) { return self . Equaltowithrelation(attribute,nslayoutrelationequal); };} |
Functional Programming Summary
- If you want to call another method, you need to return an object;
- If you want to use () to execute, then need to return a block;
- If you want the returned block to invoke the object's method again, then the block needs to return an object (that is, a block that returns a value of one object).
The concept of responsive programming
Circulating on the Internet a very classic explanation of the ' responsive programming concept '
In the development of the program:
A = B + C
After the value of B or C changes after assignment, the value of a does not change.
Responsive programming, the goal is that if the value of B or C changes, the value of a will change at the same time;
In the previous blog, "Common usage of ios-reactive cocoa"
Reactive cocoa is a classic piece of responsive programming!
Ask.-Customized IT education platform, one-to-man service for cattle, questions and answers, development and programming social Headlines official website: www.wenaaa.com
QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!
The realization of functional programming in-ios && the concept of responsive programming