美文网首页
iOS 连式语法

iOS 连式语法

作者: 孔凡伍 | 来源:发表于2018-06-06 14:10 被阅读75次

title: iOS 连式语法
date: 2016-05-04 20:58:26
tags:


@interface NSObject (LianShi)

+ (int)makeCalculate:(void (^)(CaculatorMaker *))block;

@end

@implementation NSObject (LianShi)
// 定义事件
+ (int)makeCalculate:(void (^)(CaculatorMaker *))block
{
    CaculatorMaker *caculatorMaker = CaculatorMaker.new;
    if (block) {
        block(caculatorMaker);
    }
    return caculatorMaker.all;
}

@end

@interface CaculatorMaker : NSObject

/** <##> */
@property int all;

- (CaculatorMaker *(^)(int value, int value2))add;

- (CaculatorMaker *(^)(int))sub;

@end

@implementation CaculatorMaker

- (CaculatorMaker *(^)(int value, int value2))add
{
    // return一个匿名block,参数int类型,block返回类型 CaculatorMaker *
    return ^CaculatorMaker *(int value, int value2) {
        NSLog(@"%d %d", value, value2);
        _all += value;
        return self;
    };
}

- (CaculatorMaker *(^)(int))sub
{
    return ^CaculatorMaker *(int value) {
        NSLog(@"%d", value);
        _all += value;
        return self;
    };
}

@end

// 用法
int all = [NSObject makeCalculate:^(CaculatorMaker *caculatorMaker) {
        caculatorMaker.add(2, 3).sub(5);
    }];
    NSLog(@"%d", all);


相关文章

  • iOS 连式语法

    title: iOS 连式语法date: 2016-05-04 20:58:26tags:

  • iOS链式函数式响应式编程

    iOS链式,函数式,响应式编程 block表达式语法: 作为函数参数的语法 定义block简写 作为返回值的语法 ...

  • iOS系列教程

    Swift国内社区: SwiftMic iOS系列教程 Swift语法 Swift语法入门 数据存储 iOS数据存...

  • 7 - CSS

    外连样式(推荐) html语法 @import "style2.css";css语法 内连样式 html语法 (一...

  • 11.Swift 函数

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

  • 12.Swift 类的介绍和定义

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

  • 13.Swift 构造函数

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

  • 15.Swift 懒加载

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

  • 00.Swift 介绍

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

  • 14.Swift 闭包

    @(〓〓 iOS-Swift语法)[Swift 语法] 作者: Liwx 邮箱: 1032282633@qq.c...

网友评论

      本文标题:iOS 连式语法

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