The realization of functional programming in-ios && the concept of responsive programming

Source: Internet
Author: User

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:
make.centerY.equalTo(self.view).offset(100);

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

    1. New Project Command +shift + N
    2. Create a Person class
      and add two methods to it
        (void)run;  - (void)study;
      Simple implementation:
        - (void)run{   NSLog(@"run");  }  - (void)study {   NSLog(@"study") }
    3. Instantiate and invoke the associated method
      Person*person = [[Personalloc]init]; [personrun]; [personstudy];
      The above three steps to achieve a very simple program;

Final goal

    person.runBlock().studyBlock().runBlock();    person.studyBlock().studyBlock().runBlock();

Decomposition target
1. Implement this method first to call

      [[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

-(Person*)run1;-(Person*)study1;

Implementation of the method

- (Person*)run1 {        NSLog(@"run");        return [[Personalloc] init];}- (Person*)study1{         NSLog(@"study"); return[[Personalloc]init];}

Achieve the ultimate goal:

    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

(Person* (^)())runBlock;- (Person* (^)())studyBlock;

Realize:

 - (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.

make.right.equalTo(self.right).insets(kPadding);

The function that is finally called:
Returned is a block with a return value of Masconstraint.

- (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;



Wen/ios audio and video (Jane book author)
Original link: http://www.jianshu.com/p/7017a220f34c
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

The realization of functional programming in-ios && the concept of responsive programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.