RAC parsing-custom chain programming

Keywords: iOS Attribute Programming

objective

Simulating Masonry's Continuous Use of Point Grammar

[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10).offset(1);
    }];

Write out a join operation

 make.add(10).add(10);
If you want to see the result, jump directly to the "final result"

Analysis

Definition of SQMath class

SQMath.h

#import <Foundation/Foundation.h>

@interface SQMath : NSObject

- (NSInteger)result;

- (void)add:(int)number;

@end

SQMath.m

#import "SQMath.h"

@interface SQMath ()

@property (nonatomic, assign) NSInteger number;

@end

@implementation SQMath

- (NSInteger)result {
    return self.number;
}

- (void)add:(int)number {
    self.number += number;
}

@end

Use this SQMath add method

    SQMath *math = [[SQMath alloc] init];
    [math add:10];
    [math add:20];
    NSLog(@"%ld", [math result]);
2. Change function call to point grammar

If you want to use point grammar, you need to change - add from a method to an attribute of add. But then there's no way to pass it on.

- (NSInteger)add;

But if the return value is a block of type NSInteger (^)(NSInteger), that's fine. math.add returns the block, which requires an NSInteger as a parameter (plus) and a return value of NSInteger (result).

SQMath.m

- (NSInteger (^)(NSInteger count))add {
    __weak typeof(self) weakSelf=self;
    NSInteger (^addBlock)(NSInteger) = ^(NSInteger addCount){
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.number += addCount;
        return strongSelf.number;
    };

    return addBlock;
}

perhaps

- (NSInteger (^)(NSInteger count))add {
    __weak typeof(self) weakSelf=self;
    return ^(NSInteger addCount) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.number += addCount;
        return strongSelf.number;
    };
}

Use this SQMath add method

    SQMath *math = [[SQMath alloc] init];
    NSLog(@"%ld", math.add(10));
    NSLog(@"%ld", math.add(20));
    NSLog(@"%ld", math.add(30));
3. Continuous use of point grammar

Just change the return value of the Block to self. In this way, each add returns an instance object of SQMath, so that the effect of continuous point grammar can be achieved.

- (SQMath* (^)(NSInteger count))add {
    __weak typeof(self) weakSelf=self;
    return ^(NSInteger addCount) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.number += addCount;
        return self;
    };
}

Use this SQMath add method

SQMath *math = [[SQMath alloc] init];
NSLog(@"%ld", math.add(10).add(20).add(30).result) ;
4. Change this to Category of NSNumber

NSNumber+SQMath.h

#import <Foundation/Foundation.h>
#import "SQMath.h"

@interface NSNumber (Math)

- (NSInteger)sq_add:(void(^)(SQMath *make))block;

@end

NSNumber+SQMath.m

#import "NSNumber+SQMath.h"

@implementation NSNumber (SQMath)

- (NSInteger)sq_add:(void(^)(SQMath *))block {
    SQMath *math = [[SQMath alloc] init];
    block(math);
    return math.result;
}

@end

NSNumber+SQMath

    NSInteger result = [@10 sq_add:^(SQMath * make) {
        make.add(10).add(20);
    }];
    
    NSLog(@"%ld", result);

final result

SQChainProgramming

ps: I'm not sure when chain programming will be used, but I know it's definitely useful for interviews.

Posted by very_new_user on Sun, 07 Apr 2019 16:15:30 -0700