preface

Let’s take a look at a few programming ideas that are often encountered during development

  1. Process oriented: Process oriented, step by step implementation.
  2. The idea of functional programming is to write operations as a series of nested functions or method calls.

Functional programming features: Each method must have a return value (its own object), take the function or Block as an argument, the Block argument (the value to be operated on) and the Block return value (the result of the operation).

Rep: ReactiveCocoa

  1. Object oriented: Everything is an object

The advantage of object-oriented versus process-oriented is that nominalized concepts are easier for humans to understand than verbalized descriptions. This is a major advantage over functional programming, where many functions are hard to name, but objects are easy. Review encapsulation, inheritance, polymorphism.

Encapsulation is for better reusability, scalability, but take performance into account, and even adding new judgments will increase performance, but not by magnitude. Encapsulation of simple factories and abstract factories (reflection mechanisms), for example, can increase performance costs. But it makes the management of objects configurable.

  1. The idea of chained programming: passing multiple operations (lines of code) through a dot (.) A (1).b(2).c(3).

Link programming features: the return value of the method is block. The block must have a return value (its own object). The block parameter (the value to be operated on) represents the framework for the navigation

 p.run() <=> [p run]();// The block returned by the method is executed immediately after the method is executed
Copy the code
  1. The idea of reactive programming: you don’t need to think about the order of the call, you just need to think about the result. It’s like the butterfly effect. When you produce an event, it affects a lot of things.

A. KVO is an objective-C implementation of the Observer Pattern, which is the basis for Cocoa Binding. The observer object is notified when one of the properties of the observed object changes. B. Reactive Cocoa framework

I, examples of chained programming


1.1 Two forms of chaining programming

  1. Declare a property whose type is block. A block must have a return value (the object itself), and a block parameter (the value to be operated on).
  2. Declaration method: The downside is that Xcode prompts are not very friendly. Is not recommended

The essence is the same, after executing the method, you get a block. The block must have a return value (the object itself), and the block argument (the value to be operated on).

1.2 Realize chain programming by declarative method form

The downside is that Xcode prompts are not very friendly. Don’t recommend `

Example 1 :(combining the properties of block and method) github.com/zhangkn/DKU…

The return value of a direct declaration method is a block. The block must have a return value (the object itself), and the block argument (the value to be operated on).

Example: p.r UN () < = > [p run] () effect: p.r UN () < = > [p run] (); // The block returned by the method is executed immediately after the method is executed.

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        // Use block to implement chained programming
        People *p=  [[People alloc]init];
// p.run() <=> [p run](); // The block returned by the method is executed immediately after the method is executed.
        p.run().study();
        
        p.run().name(@" Combine the properties of blocks and methods.");
        
        
    }
    return 0;
}
Copy the code

Code implementation

#import "People.h"

@implementation People

- (People *(^)())run{
    return^ {NSLog(@"run");
        return self;
    };// Return a block
}


- (People *(^)())study{
    return^ {NSLog(@"study");
        return self;
    };// Return a block
}


- (People *(^)(NSString *))name{
    return^ (NSString *name){
        NSLog(@ "% @",name);
        return self;
    };
}

Copy the code

1.3 Realize chain programming by declaring attributes

Declare a property whose type is block. A block must have a return value (the object itself), and a block parameter (the value to be operated on).

Declare a property: this is essentially using its getter method

Full SDK source code [encapsulate rich text API, using block to achieve chain programming] (block magic: combined with the advantages of block and method to achieve iOS chain programming)

  1. Article: blog.csdn.net/z929118967/…

  2. Resources from CSDN download the complete SDK code: https://download.csdn.net/download/u011018979/14038715

  3. Part of the API

  1. Usage example

    NSMutableAttributedString *xx  = [[NSMutableAttributedString alloc]init];
    
    
    xx.kn_addString(@" Encapsulating rich text API").kn_fontColor(UIColor.redColor).kn_addString(@" Chaining programming with Blocks").kn_fontColor(UIColor.blueColor).kn_addString(@! "");

Copy the code

Github.com/zhangkn/Cha…

Taking the framework of navigation as an example, the analysis is carried out

The equalTo: method called returns a block with the value MASConstraint

- (MASConstraint * (^)(id))equalTo { return ^id(id attribute) { return self.equalToWithRelation(attribute, NSLayoutRelationEqual); }; }
Copy the code

usage

- (UIImageView *)imgForReason{
 
    if (nil == _imgForReason) {
 
         
 
        UIImageView *img2 = [[UIImageView alloc] init];
 
        _imgForReason = img2;
 
        [self addSubview:_imgForReason];       
 
        [img2 mas_makeConstraints:^(MASConstraintMaker *make) {
 
            make.left.equalTo(self.mas_left).offset(25);
 
            make.top.equalTo(self.imgForPassword.mas_bottom).offset(12);
 
            make.size.mas_equalTo(CGSizeMake(kScreenWidth- 100..76));
 
             
 
        }];
 
         
 
    }
 
    return _imgForReason;
 
}
Copy the code

see also

IOS Rich text/text mixed guide

1, encapsulate rich text API, using block to achieve chain programming

2. Hyperlink properties

3. Translate HTML strings to rich text

4. Create rich text with images

— — — — — — — —

Copyright Notice: This article is the original article of “# public account: iOS Reverse”. It follows the COPYRIGHT agreement of CC 4.0 by-SA. Please attach the link of the original source and this notice.

The original link: blog.csdn.net/z929118967/…