In-depth iOS functional programming and responsive programming concepts

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 masonry && Snapkit source code has certain help.

Picture description

As an iOS developer, you must have used masnory/snapkit;

Masonry is a third-party framework for an OC-developed, very easy-to-use automatic layout; Snapkit is a swift version of the automated layout framework created by the masonry team; If you have not used it, the original or native version of the VFL language is used in the automatic layout, then I have to point "Great location!"

A typical masonry statement:

Make.centerY.equalTo (Self.view). offset (100); That's the "." Let 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 creates a person class and adds two methods to it:-(void) run; -(void) study; Simple implementation:

-(void) run{NSLog (@ "Run"); }-(void) Study {NSLog (@ "study")} to instantiate and invoke the associated method:

Person*person = [[Personalloc]init];  [Personrun]; [Personstudy]; The above three steps implement a very simple program.

Final goal:

Person.runblock (). Studyblock (). Runblock (); Person.studyblock (). Studyblock (). Runblock (); Decomposition target:

1. This method of invocation is implemented first:

[[Person run1] study1]; 2. Achieve the ultimate goal:

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]; To achieve the ultimate goal:

Person.runblock (). Studyblock (). Runblock (); In OC, the ' () ' block is executed in the form of (), assuming that if a block is returned, then I can use () 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:

Declaration-(person* (^) ()) Runblock; -(person* (^) ()) Studyblock; Implementation  -(person* (^) ()) Runblock {         person* (^block) () = ^ () {                              NSLog (@ "Run");                          return self;                };              return block; }  -(person* (^) ()) Studyblock {       person* (^block) () = ^ ()   {  & nbsp;                        NSLog (@ "study");               return self;               };         return block; And 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 (Attri     Bute,nslayoutrelationequal); }; }

Functional Programming Summary

If you want to call another method, you need to return an object, and if you want to execute it, you need to return a block, and if you want the returned block to invoke the object's method again, the block needs to return an object (that is, a block that returns an object).

The concept of responsive programming

Circulating on the Internet a very classic interpretation of the concept of "responsive Programming": In program development, 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-depth iOS functional programming and responsive programming concepts

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.