iOS-OC creates chain syntax (chain programming)

foreword


I used a chain grammar in my project to write a simple interest. There is a small partner who is not very clear about the chain grammar, and instantly feels that the chain grammar is high; we often see the chain grammar in some third-party libraries. . When it comes to the benefits of the chain syntax, there are the following points:
1. I am used to the OC bracket syntax, the dot syntax gives me a refreshing feeling, the program can write a little novelty, and the dot syntax is the earliest and most basic syntax structure, namely : Object name. Member variable;
2. The chain syntax is more readable, close to natural language, and the function of this method can be roughly known directly through the method name;
3. Streamlined code;

actual combat

Let's take a look at a small demo I wrote first, taking the simple-interest chain syntax as an example:

Zvc and ZFJObjectShareInstance () are the instantiated objects of ZFJObject , "." is the continuous access to this instantiated object, and the access is to some methods of this object, such as the number in add ( 20 ) is a parameter.


code

Inside ZFJObject.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface ZFJObject : NSObject

//create simple interest
+ (instancetype)shareInstance;

//destroy the store
+ (void)destroyInstance;

//addition
@property (nonatomic,copy) ZFJObject *(^ add)(CGFloat num);

//subtract
@property (nonatomic,copy) ZFJObject *(^ minus)(CGFloat num);

//multiplication
@property (nonatomic,copy) ZFJObject *(^ multiply)(CGFloat num);

//division
@property (nonatomic, copy) ZFJObject * (^divide)(CGFloat num);

// get the result of the operation
@property (nonatomic, copy) ZFJObject * (^getResult)(void(^ getResultBlock)(CGFloat result));

ZFJObject * ZFJObjectShareInstance(void);

@end

Inside ZFJObject.m

#import "ZFJObject.h"

@interface ZFJObject ()

@property (nonatomic,assign) CGFloat result;//Save the result

@end

static ZFJObject *_manager = nil;

@implementation ZFJObject

+ (instancetype)shareInstance{
    
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

+ (void)destroyInstance {
    _manager = nil;
}

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

/**
 addition
 */
- (ZFJObject *(^)(CGFloat num))add{
    return ^(CGFloat num) {
        self.result += num;
        return [ZFJObject shareInstance];
    };
}

/**
 subtraction
 */
- (ZFJObject *(^)(CGFloat num))minus{
    return ^(CGFloat num) {
        self.result -= num;
        return [ZFJObject shareInstance];
    };
}

/**
 multiplication
 */
- (ZFJObject *(^)(CGFloat num))multiply{
    return ^(CGFloat num) {
        self.result *= num;
        return [ZFJObject shareInstance];
    };
}

/**
 division
 */
- (ZFJObject *(^)(CGFloat num))divide{
    return ^(CGFloat num) {
        NSAssert(num != 0, @"Divisor cannot be zero!");
        self.result /= num;
        return [ZFJObject shareInstance];
    };
}

/**
 result
 */
- (ZFJObject *(^)(void(^ getResultBlock)(CGFloat result)))getResult{
    return ^(void(^ getResultBlock)(CGFloat result)) {
        if(getResultBlock){
            getResultBlock(self.result);
        }
        return [ZFJObject shareInstance];
    };
}

- (void)dealloc{
    NSLog(@"dealloc");
}

@end


ZFJObject * ZFJObjectShareInstance(void){
    return [ZFJObject shareInstance];
}

transfer

ZFJObjectShareInstance().add(20).minus(2).multiply(2).divide(10).getResult(^(CGFloat result) {
        NSLog(@"result == %f",result);
    });

illustrate

If we want to initialize an object ourselves, we only need a little modification to implement the chaining syntax, such as:

/**
 addition
 */
- (ZFJObject *(^)(CGFloat num))add{
    return ^(CGFloat num) {
        self.result += num;
        return [ZFJObject shareInstance];
    };
}

Change it to:

/**
 addition
 */
- (ZFJObject *(^)(CGFloat num))add{
    return ^(CGFloat num) {
        self.result += num;
        return self;
    };
}

Then call:

ZFJObject *zvc = [[ZFJObject alloc]init];
    zvc.add(20).minus(2).multiply(2).divide(10).getResult(^(CGFloat result) {
        NSLog(@"result == %f",result);
    });

I recommend a chained grammar constraint library, written by a friend, which is simple and easy to use. You don’t need to write constraints yourself, which improves development efficiency.

https://github.com/netyouli/WHC_AutoLayoutKit




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325590115&siteId=291194637