A preliminary study on Reactivecocoa chain-type programming

Source: Internet
Author: User

When using the masonry framework for automatic layout, a layout is interspersed with 6 lines of code in the program.

[View mas_makeconstraints:^ (Masconstraintmaker *make) {

Make.top.equalTo (Anotherview);

Make.left.equalTo (Anotherview);

Make.width.mas_equalTo (@60);

Make.height.mas_equalTo (@60);

}];

Always feel not pretty enough to have a line of code to set the constraints of the framework, I have tried to encapsulate a category on the masonry uiview+hksetconstraints, use it is not comfortable, always feel not enough flavor, until I met Sdautolayout, The real implementation of a code to achieve automatic layout, coupled with the study Reactivecocoa when you see the fastest Reactivecocoa basic article (below will give a link) mentioned programming ideas, only to understand masonry and Sdautolayout a little realization of ideas: chain-based programming ideas, share their own experience, hope is what you like, PS: This is my first time to share the article, write bad please point out, the next good correction.

The next part is excerpted from: The quickest to get you started Reactivecocoa basic

Let's start with a brief introduction to the programming ideas we now know.

1 process-oriented: handle things with process as the core, step by step implementation.

2 Object-oriented: everything objects

3 Chain Programming Idea: is to pass multiple operations (multiple lines of code) through the dot number (.) Linking together becomes a code that makes the code readable. A (1). B (2). C (3)

    • Chained programming Features: The return value of the method is Block,block must have a return value (itself object), the block parameter (the value to be manipulated)

    • Representative: Masonry framework.

4 responsive programming thought: No need to consider the sequence of calls, just to know the results, similar to the butterfly effect, produce an event, will affect a lot of things, these events like the flow of the spread out, and then affect the results, borrowing object-oriented sentence, everything is a stream.

    • Representative: KVO use.

5 Functional Programming Idea: is to write the operation as much as possible into a series of nested functions or method calls.

    • Functional Programming Features: Each method must have a return value (itself object), the function or block as a parameter, the block parameter (the value to be manipulated) block return value (operation result)

    • Representative: Reactivecocoa.

    • With functional programming, write an addition calculator, and the addition calculator comes with a judge whether it equals a value.

Here we implement a calculator with a chain-coded idea code:

#import

@class Caculatormaker;

@interface NSObject (Caculatormaker)

Calculation

+ (int) Makecaculators: (void (^) (Caculatormaker *make)) Caculatormaker;

@end

#import "Nsobject+caculatormaker.h"

#import "CaculatorMaker.h"

@implementation NSObject (Caculatormaker)

Calculation

+ (int) Makecaculators: (void (^) (Caculatormaker *make)) block

{

Caculatormaker *mgr = [[Caculatormaker alloc] init];

Block (MGR);

return mgr.iresult;

}

@end

#import

@interface Caculatormaker:nsobject

@property (nonatomic, assign) int iresult;

Addition

-(Caculatormaker * (^) (int)) add;

Subtraction

-(Caculatormaker * (^) (int)) sub;

Multiplication

-(Caculatormaker * (^) (int)) muilt;

Division

-(Caculatormaker * (^) (int)) divide;

@end



#import "CaculatorMaker.h"

@implementation Caculatormaker

-(Caculatormaker * (^) (int)) add

{

return ^ (int value)

{

_iresult + = value;

return self;

};

}

@end

Call:

int iresult = [NSObject makecaculators:^ (Caculatormaker *make) {

Make.add (1). Add (2). Add (3). Divide (2);

}];


Analyze This method to execute the process:

The first step: NSObject creates a block that creates a Caculatormaker object make and returns

Step two: This object makes the call method Add, which holds the property iresult does an addition, and returns itself so that it can continue to invoke the method.

This is a small but clear example of the idea of chained programming.

Now we take masonry example:

Let's see masonry.

-(Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *make)) block;

-(Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *)) block {

Self.translatesautoresizingmaskintoconstraints = NO;

Masconstraintmaker *constraintmaker = [[Masconstraintmaker alloc] initwithview:self];

Block (Constraintmaker);

return [constraintmaker install];

}

Is it the same as our calculator category?????

Let's look at it again.

-(Masconstraint * (^) (id attr)) mas_equalto;

-(Masconstraint * (^) (id)) Mas_equalto {

return ^id (id attribute) {

return Self.equaltowithrelation (attribute, nslayoutrelationequal);

};

}


Take a look at its self.equaltowithrelation implementation:

-(Masconstraint * (^) (ID, nslayoutrelation)) Equaltowithrelation {

return ^id (id attribute, nslayoutrelation relation) {

if ([attribute IsKindOfClass:NSArray.class]) {

Nsassert (!self.haslayoutrelation, @ "redefinition of constraint relation");

Nsmutablearray *children = nsmutablearray.new;

For (id attr in attribute) {

Masviewconstraint *viewconstraint = [self copy];

Viewconstraint.secondviewattribute = attr;

[Children Addobject:viewconstraint];

}

Mascompositeconstraint *compositeconstraint = [[Mascompositeconstraint alloc] initwithchildren:children];

Compositeconstraint.delegate = self.delegate;

[Self.delegate constraint:self Shouldbereplacedwithconstraint:compositeconstraint];

return compositeconstraint;

} else {

Nsassert (!self.haslayoutrelation | | self.layoutrelation = = Relation && [attribute IsKindOfClass:NSValue.class ] @ "redefinition of constraint relation");

Self.layoutrelation = relation;

Self.secondviewattribute = attribute;

return self;

}

};

}


It is true that it is returning to itself, so this is the embodiment of its chain-programming idea.

Because I did not continue to study masonry, the study of God's classmates can continue to share its more detailed ideas.

On the chain of programming, I hope that the students have not touched the next package similar class, you can think in this direction, less detours, thank you

A preliminary study on Reactivecocoa chain-type 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.