码迷,mamicode.com
首页 > 其他好文 > 详细

模仿Masonary写一个计算器

时间:2017-07-20 15:22:52      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:pre   技术分享   png   return   elf   void   计算   src   maker   

技术分享

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写一个计算器

标签:pre   技术分享   png   return   elf   void   计算   src   maker   

原文地址:http://www.cnblogs.com/hualuoshuijia/p/7210925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!