美文网首页程序员iOS面试
1.RAC解析 - 自定义链式编程

1.RAC解析 - 自定义链式编程

作者: sqatm | 来源:发表于2019-04-05 00:39 被阅读0次

目的

模仿Masonry连续运用点语法的操作

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

写出一个连加的操作

 make.add(10).add(10);

想看结果的请直接跳到“最终结果”

分析

一.定义SQMath类

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

使用这个SQMath的add方法

    SQMath *math = [[SQMath alloc] init];
    [math add:10];
    [math add:20];
    NSLog(@"%ld", [math result]);
二.将函数调用改为点语法

如果要用点语法,需要让-add从一个方法变成一个add的属性。但是这样就没有办法传参了

- (NSInteger)add;

但是如果返回值是一个 NSInteger (^)(NSInteger) 类型的block就可以了。math.add返回的是这个block,这个block是需要一个NSInteger为参数(加数),返回值是NSInteger(结果)。

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;
}

或者

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

使用这个SQMath的add方法

    SQMath *math = [[SQMath alloc] init];
    NSLog(@"%ld", math.add(10));
    NSLog(@"%ld", math.add(20));
    NSLog(@"%ld", math.add(30));
三.连续使用点语法

只要将Block的返回值更改为self。这样每次add返回的则变成了SQMath的实例对象,这样就可以实现连续点语法的效果了。

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

使用这个SQMath的add方法

SQMath *math = [[SQMath alloc] init];
NSLog(@"%ld", math.add(10).add(20).add(30).result) ;
四.将这个改为NSNumber的Category

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);

最终结果

SQChainProgramming

ps:链式编程什么时候用我还真不太清楚,但我知道面试的时候肯定有用 哈哈。

相关文章

  • 1.RAC解析 - 自定义链式编程

    目的 模仿Masonry连续运用点语法的操作 写出一个连加的操作 想看结果的请直接跳到“最终结果” 分析 一.定义...

  • 链式编程

    概念解析: 链式编程:通过点将函数调用连接起来就是链式编程。《它的特点是一大串的链接起来》 函数编程:通过 obj...

  • jQuery 链式编程

    jQuery 链式编程 链式编程原理 链式编程代码示例 隐式迭代

  • 链式编程思想

    链式编程思想 链式编程思想

  • iOS 链式编程

    链式编程 主要介绍链式编程原理,以及如何创建链式编程 编程范式 在介绍链式编程之前,首先来了解下什么是编程范式。 ...

  • iOS开发Masonry框架源码解析

    iOS开发Masonry框架源码解析 前言:这个框架编程思想主要包括链式编程 这是一个iOS在控件布局中的轻量级框...

  • 四月二周技术复盘

    idea的get方法修改 1 set/get生成方法时,不去掉is2 链式编程 设置自定义get方法 设置自定义s...

  • ReactiveObjC入门

    ReactiveObjC基础用法 iOS开发三种编程方式(响应式编程、函数编程、链式编程),函数编程最常用,链式编...

  • 工作iOS技术总结

    链式编程、函数式、面向接口编程思想 iOS 之ReactiveCocoa 链式编程2 WKWebView的缓存处理...

  • 一、链式编程思想、响应式编程思想和函数式编程

    1. 链式编程思想 链式编程思想的核心就是方法的返回值必须是Block,Masonry是链式编程思想应用的代表。 ...

网友评论

    本文标题:1.RAC解析 - 自定义链式编程

    本文链接:https://www.haomeiwen.com/subject/iiwdvqtx.html