1.CaculatorMaker
@interface CaculatorMaker : NSObject @property(nonatomic,assign)int result; -(CaculatorMaker *(^)(int))add; -(CaculatorMaker *(^)(int))sub; -(CaculatorMaker *(^)(int))muilt; -(CaculatorMaker *(^)(int))divide; @end
#import "CaculatorMaker.h" @implementation CaculatorMaker - (CaculatorMaker *(^)(int))add { return ^(int value) { self.result += value; return self; }; } -(CaculatorMaker *(^)(int))sub{ return ^CaculatorMaker *(int value){ _result -= value; return self; }; } -(CaculatorMaker *(^)(int))muilt; { return ^CaculatorMaker *(int value){ _result *= value; return self; }; } -(CaculatorMaker *(^)(int))divide; { return ^CaculatorMaker *(int value){ _result /= value; return self; }; } @end
NSObject (CaculatorMaker)
#import <Foundation/Foundation.h> #import "CaculatorMaker.h" @interface NSObject (CaculatorMaker) //计算 + (int)makeCaculators:(void(^)(CaculatorMaker *make))caculatorMaker; @end
#import "NSObject+CaculatorMaker.h" @implementation NSObject (CaculatorMaker) //计算 + (int)makeCaculators:(void(^)(CaculatorMaker *make))block { CaculatorMaker *mgr = [[CaculatorMaker alloc] init]; block(mgr); return mgr.result; } @end
使用:
int iResult = [NSObject makeCaculators:^(CaculatorMaker *make) { make.add(1).add(2).add(3).divide(2); }]; NSLog(@"-==========:%d",iResult);