In functional languages, functions, as first-class citizens, can be defined anywhere, inside or outside a function, as arguments and return values to a function, and can be combined. One of the most common examples of functional programming in iOS, I would like to announce the benefits of chained programming (sequential writing of the code to be performed by “.”). I would like to control the constraint conditions of the control I would like to write by means of point syntax

make.centerY.equalTo(self.view).offset(100);
Copy the code

FuncControl A simple and functional way of writing a basic control


##### how to understand functional programming and chain programming to the commonly used UITextField as an example, before setting each property we need to let the object UITextField to achieve its corresponding method, so we need to write down such layers

    UITextField *textField=[UITextField new];
    textField.frame=CGRectMake(10.200.200.40);
    textField.text=@"textField";
    textField.placeholder=@"placeholder";
    textField.textColor=[UIColor grayColor];
    [self.view addSubview:textField];
Copy the code

The other way to think about it, if UITextField returns UITextField for each property setting, we can write all the method Settings we need at once, like a chain. Frame.text.placeholder

UITextField *textField=[UITextField new].func_frame(CGRectMake(10.200.200.40)).func_text(@"textField").func_placeholder(@"placeholder").func_borderStyle(UITextBorderStyleLine).func_clearButtonMode(UITextFieldViewModeWhileEditing);
[self.view addSubview:textField];
Copy the code

Func_text (@”textField”) uses () to call functions, which is functional programming. The classic example of making.centery. EqualTo (self.view) would be all I would retrieve

Func_text (@”textField”) executes a block, and if the return value of that block is an object, the link will continue forever.

/ / declare
@interface UITextField (FuncChains)

- (UITextField * (^) (NSString *text))func_text;


/ / implementation
@implementation UITextField (FuncChains)

- (UITextField * (^) (NSString *text))func_text{
    return ^id(NSString *text){
        self.text=text;
        return self;
    };
}

Copy the code

If this is still a tricky implementation, think of it as the text property assigned to the textField in a block executed in func_text, and then return the textField object

// Implement the same method
- (UITextField * (^) (NSString *text))func_text{
    UITextField *(^textFieldBlock)(NSString *text)= ^(NSString *text){
        self.text=text;
        return self;
    };
    return textFieldBlock;
}
Copy the code

FuncControl A simple and functional way of writing a basic control


Finally, I’ll recall whether functional programming in navigation makes it clear that if you wish to perform another task after performing a method, you’ll need to return the object that performs the method, i.e., an object in the performing block

/** * Aliases to corresponding relation methods (for shorthand macros) * Also needed to aid autocompletion */
- (MASConstraint * (^)(id attr))mas_equalTo;

- (MASConstraint * (^)(id))mas_equalTo {
    return ^id(id attribute) {
        return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
    };
}
Copy the code
/** * Modifies the NSLayoutConstraint constant */
- (MASConstraint * (^) (CGFloat offset))offset;

- (MASConstraint * (^) (CGFloat))offset {
    return ^id(CGFloat offset){
        self.offset = offset;
        return self;
    };
}
Copy the code