首页 > 代码库 > 模仿Masonary写一个计算器
模仿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写一个计算器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。