2018-11-04iOS链式响应编程-加减乘除

一、什么是链式编程?
链式编程,是指将多个方法用点语法链接起来。
1)让代码更加简洁;
2)可读性更强;
3)编程性强;
4)对程序员的业务能力要求高;
5)不太利于代码调试。 
二、调用代码,如下:
//
//  ViewController.m
//  链式编程Demo
//
//  Created by ghk on 2018/11/4.
//  Copyright © 2018年 ghk. All rights reserved.
//

#import "ViewController.h"
#import "Tool.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    float result = [Tool make:^(Tool *tool) {

        tool.add(1).add(2).add(3).subtract(1).ride(5).divide(4);
    }];

    NSLog(@"%f",result);
}


@end

三、Tool实现代码,如下:
//
//  Tool.h
//  链式编程Demo
//
//  Created by ghk on 2018/11/4.
//  Copyright © 2018年 ghk. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Tool : NSObject

typedef Tool * (^Block)(NSInteger number);

/** 加法 */
@property (nonatomic, copy, readonly) Block add;
/** 减法 */
@property (nonatomic, copy, readonly) Block subtract;
/** 乘法 */
@property (nonatomic, copy, readonly) Block ride;
/** 除法 */
@property (nonatomic, copy, readonly) Block divide;

+ (float)make:(void (^)(Tool *tool))block;

@end
//
//  Tool.m
//  链式编程Demo
//
//  Created by ghk on 2018/11/4.
//  Copyright © 2018年 ghk. All rights reserved.
//

#import "Tool.h"

@interface Tool ()

@property (nonatomic, assign) float result;

@end

@implementation Tool

- (void)dealloc
{
    NSLog(@"%s",__func__);
}

+ (float )make:(void (^)(Tool *tool))block {

    if (block) {

        Tool *tool = [[Tool alloc] init];
        block(tool);
        return tool.result;
    }
    return 0;
}

- (Block)add {

    return ^(NSInteger number) {

        float newResult = self.result;
        newResult += number;
        NSLog(@" %f + %zi = %f",self.result,number,newResult);
        self.result = newResult;

        return self;
    };
}

- (Block)subtract {

    return ^(NSInteger number) {

        float newResult = self.result;
        newResult -= number;
        NSLog(@" %f - %zi = %f",self.result,number,newResult);
        self.result = newResult;

        return self;
    };
}

- (Block)ride {

    return ^(NSInteger number) {
        float newResult = self.result;
        newResult *= number;
        NSLog(@" %f × %zi = %f",self.result,number,newResult);
        self.result = newResult;
        return self;
    };
}

- (Block)divide {

    return ^(NSInteger number) {
        float newResult = self.result;
        newResult /= number;
        NSLog(@" %f ÷ %zi = %f",self.result,number,newResult);
        self.result = newResult;
        return self;
    };
}

@end
欢迎评价!!!!!!
感谢支持!!!!!!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,993评论 2 59
  • 终于等到你,激动人心的时刻!虽然是最热的伏天,身体最不适的季节,但我还是期盼许久这天的到来!每年暑假可以出行的旅行...
    雨馨lily阅读 502评论 3 6
  • 一,难忘岁月 己过知命之年 朝霞映照眉间 韶光虽己川逝 葱绿永驻心田 假山钟声荡耳 望湖朗诵动天 彈指四十秋度 苍...
    晓村阅读 307评论 0 0