模仿Masonary写一个计算器
Posted 新年新气象934060369
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模仿Masonary写一个计算器相关的知识,希望对你有一定的参考价值。
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);
以上是关于模仿Masonary写一个计算器的主要内容,如果未能解决你的问题,请参考以下文章