链式编程思想

2017-03-30  本文已影响14人  大虾咪
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    
    make.top.left.equalTo(@10);
    make.right.bottom.equalTo(@-10);
    
}];

int result = [self tmf_calculate:^(CalcalateManager *mgr){
    
    mgr.add(5).add(6);
    
}];

NSLog(@"result:%d",result);
  #import "NSObject+Calculate.h”
  #import “CalcalateManager.h"
@implementation NSObject (Calculate)

-(int)tmf_calculate:(void(^)())block{
    // 创建计算管理者
    CalcalateManager *mgr = [[CalcalateManager alloc] init];
    // 执行计算
    block(mgr);
    return mgr.result;
}
#import <Foundation/Foundation.h>

@interface CalcalateManager : NSObject
@property(nonatomic, assign) int result;

- (CalcalateManager *(^)(int))add;

@end


#import "CalcalateManager.h"

@implementation CalcalateManager

- (CalcalateManager *(^)(int))add{
    
    return ^(int value){
        
        _result += value;
        return self;
        
    };
    
}

@end
上一篇下一篇

猜你喜欢

热点阅读