preface

In the development process, we often set parameters for the properties of an object for many times. For example, when setting a UILable and UIButton, we will use the setter and getter properties of dot syntax for many times, and the steps are tedious. Then I think of the chained programming structure used in SDAutoLayout and navigation, and many mature third-party frameworks are using it more and more.

The biggest advantage of this programming method is the one-time development, and the subsequent use is quick call, eliminating the tedious multi-call point grammar, but it also has disadvantages, the initial development of a large amount of code, complex grammar. But for some commonly used classes, it’s a lifetime benefit.

Method of use

I’ll retrieve it for you

How to use SDAutoLayout

Realize the principle of

Through the above two examples, we try to guess the general structure:

  1. Chain syntax calls dot syntax every time it is called.
  2. And because OC calls are usually called with brackets, it can be assumed that a Block is called,
  3. And because the syntax of chained programming is to call consecutively, and many times to call methods consecutively, we can assume that each call should return an instance object of the class.

Let’s first look at the implementation of the above two

Masonry

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

SDAutoLayout

typedef SDAutoLayoutModel *(^MarginToView)(id viewOrViewsArray, CGFloat value); // Return value of getter method - (MarginToView)leftSpaceToView {if(! _leftSpaceToView) { _leftSpaceToView = [self marginToViewblockWithKey:@"left"];
     }
     return _leftSpaceToView;
 }

 - (MarginToView)marginToViewblockWithKey:(NSString *)key
 {
     __weak typeof(self) weakSelf = self;
     return ^(id viewOrViewsArray, CGFloat value) {
     SDAutoLayoutModelItem *item = [SDAutoLayoutModelItem new];
     item.value = @(value);
     if ([viewOrViewsArray isKindOfClass:[UIView class]]) {
         item.refView = viewOrViewsArray;
     } else if ([viewOrViewsArray isKindOfClass:[NSArray class]]) {
         item.refViewsArray = [viewOrViewsArray copy];
     }
     [weakSelf setValue:item forKey:key];
     return weakSelf;
   };
 }Copy the code

From the above we can see that it is basically the same as we guessed: point syntax is used, Block callbacks are used, and the return value is the instance object of the call.

Let’s do a quick analysis

Take the approach to navigation as an example

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

The above method can be used as a getter method, because in the process of using dot syntax, this method must return parameters, but no parameters, so this method can be used as the getter method for the offset property, that is, it can be called directly using dot syntax.

How is offset passed in? .offset(5), (5) is the argument to the entire Block and is passed out as an argument to the Block, because the return value argument of the Block is used to pass the result of the return argument from the outside to the inside, which is why we get the value of the argument inside when we pass in the argument from the outside.

The returned argument is a Block, MASConstraint is the type of the Block return value, and CGFloat is the type of the Block return argument. These are the basic uses of Block. If you don’t understand them, take a look at my reference file below.

Let’s examine the invocation process

  1. Make. Left. Offset yields a Block;
  2. Make.left.offset (5) After passing in the argument, the Block returns an object with a MASConstraint value, so you can continue calling other MASConstraint methods.

Usage scenarios

I think there are many use scenarios, depending on personal needs, but I summed up the following points:

  1. You need to get the return value and then go on to the next operation, such as the addition, subtraction, multiplication and division calculator.
  2. It is necessary to set the parameters of an instance consecutively, such as the properties of a UILable, rather than a long string of multiple points of syntax to set the properties.

But in summary, if you want to do continuous and repeated operations on an object you need to use the syntax of chain programming.

Reference recommended items

  • JHChainableAnimations
  • Masonry
  • SDAutoLayout

reference

  • Talk about the implementation of Objective-C chained syntax
  • Block get started and easy to use
  • Objective-c chain development

In addition

  • Jane’s address book
  • Denver address