Objective – C syntax

In Objective-C, the syntax is much closer to natural language in many languages, and the code is much more readable. But everything is two-sided. Instead of using the point-chain syntax of a typical development language, he uses the bracketed chain syntax. This leads to very long-winded and very long.

OC parenthesis syntax: [[[object aFunc] bFunc] cFunc:@"String"] dotted chain syntax object.afunc (1).bfunc (1).cfunc (" String")Copy the code

An introduction to objective-C chained syntax

There are roughly three types of syntax in OC.

  1. Dot syntax [.] : Generally used to get and set attributes.
  2. Brackets [[]] : generally used for method calls.
  3. Parenthesis [()] : Usually a call to a block.

Infinite point chain implementation principle

We know from the syntax of OC that to implement point-chain syntax, we must use blocks.

A Block is an anonymous function or function pointer that can take arguments and return values.

So just set the return value of the Block to the instance of the current class itself, and you can make infinite calls.

object.func1().func2(1).func(@"string")
Copy the code

This can be done in two ways:

  1. Define read-only Block properties, set the return value type to the current class itself, and then implement getter methods for those Block properties.
  2. Define the method, and then pass the method call.

Let’s go straight to the code:

DFPeople.h

// // DFPeople.h // ChainProgramming // // Created by raymond on 2021/3/24. // #import <Foundation/Foundation.h> @interface DFPeople: NSObject // The first method // defines read-only Block properties and sets the return value type to the current class itself, and then implements getter methods for those Block properties. @property (readonly, nonatomic, copy) DFPeople * (^salary)(NSInteger salary); // The second method // defines the method, and then passes the method call. -(DFPeople* (^)(NSString *))myName; @endCopy the code

DFPeople.m

//
//  DFPeople.m
//  ChainProgramming
//
//  Created by raymond on 2021/3/24.
//

#import "DFPeople.h"

@interface DFPeople()

@property (nonatomic, assign) NSInteger _salary;

@property (nonatomic, copy) NSString *name;
@end

@implementation DFPeople

-(instancetype) init{
    self = [super init];
    if(self){
    }
    return self;
}

-(DFPeople* (^)(NSString *))myName{
    
    return ^id(NSString *name){
        self.name = name;
        NSLog(@"my name is %@",(self.name));
        return self;
    };
}

-(DFPeople * (^)(NSInteger salary))salary{
    
    return ^id(NSInteger salary){
        self._salary += salary;
        NSLog(@"my salary is %@",(self._salary));
        return self;
    };
}
@end

Copy the code

Call:

// // ViewController.m // ChainProgramming // // Created by raymond on 2021/3/24. // #import "ViewController.h" #import "DFPeople.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad];  DFPeople *people = [DFPeople new]; people.myName(@"Raymond").salary(1000); } @endCopy the code

Analysis of the

Dfpeople. h // in the dfpeople. h header file, declare a copy block variable. // Set it to readonly to limit the implementation of getter methods and to prevent external modification. // The Block attribute takes an ARGUMENT of type NSInteger, named SALARY (), and its return value is the current DFPeople, @property (readOnly, nonatomic, copy) DFPeople * (^salary)(NSInteger salary); Dfpeople. m // in the dfpeople. m header, the method that implements the getter for the Block in the.h file // for the returned Block, the return type is DFPeople, so the Block returns self. -(DFPeople * (^)(NSInteger salary))salary{ return ^id(NSInteger salary){ self._salary += salary; NSLog(@"my salary is %@",(self._salary)); return self; }; [[DFPeople new].salary(1000); And then return a Block. Return ^id(NSInteger salary){self._salary += salary; NSLog(@"my salary is %@",(self._salary)); return self; }; When the Block is called, self (the class instance itself) is returned as follows: (1) dfPeople. salary -> gets a Block; Salary (1000) -> Block execution and returns self (calc) (3) dfPeople.salary (1000) can then continue to access the other properties of DFPeople, implementation of infinite point chain.Copy the code

A little bit of perspective

In this implementation of point-chain syntax, I prefer to define block variables. Because when this is used, Xcode will give the code a hint. If the method definition pattern is used, there is no code prompt. As shown in figure:

A variable that defines a block

Method definition pattern

The completion of

This concludes OC’s implementation of point-chain syntax. If it helps, please give it a thumbs up. thank you

Finally, I wish everyone, the code is always bug-free, happy life ~~~