一 方法的实现
#import <Foundation/Foundation.h>
@interface RJBlockTool : NSObject
//计算
+ (CGFloat)makeCalculator:(void(^)(RJBlockTool *rj_tool))block;
typedef RJBlockTool *(^RJCalculator)(CGFloat num);
@property (nonatomic, strong, readonly) RJCalculator plus;//
@property (nonatomic, strong, readonly) RJCalculator subtract;
@property (nonatomic, strong, readonly) RJCalculator multiply;
@property (nonatomic, strong, readonly) RJCalculator divide;
//拼接字符串
+ (NSString *)makeAppendingString:(void(^)(RJBlockTool *rj_tool))block;
@property (nonatomic, strong, readonly) RJBlockTool *(^date)(NSString *str);
@property (nonatomic, strong, readonly) RJBlockTool *(^who)(NSString *str);
@property (nonatomic, strong, readonly) RJBlockTool *(^note)(NSString *str);
@end
#import "RJBlockTool.h"
@interface RJBlockTool()
//1 计算的结果
@property(nonatomic,assign) CGFloat resultCalculator;
//2 处理后的字符串
@property(nonatomic,strong) NSString *resultString;
@end
@implementation RJBlockTool
/*************************** 计算(加减乘除) *******************************/
+(CGFloat)makeCalculator:(void (^)(RJBlockTool *))block{
if (block) {
RJBlockTool *tool = [[RJBlockTool alloc] init];
block(tool);
return tool.resultCalculator;
}
return 0;
}
-(RJCalculator)plus{
return ^(CGFloat num){
self.resultCalculator += num;
return self;
};
}
-(RJCalculator)subtract{
return ^(CGFloat num){
self.resultCalculator -= num;
return self;
};
}
-(RJCalculator)multiply{
return ^(CGFloat num){
self.resultCalculator *= num;
return self;
};
}
-(RJCalculator)divide{
return ^(CGFloat num){
self.resultCalculator /= num;
return self;
};
}
/*************************** 计算(加减乘除) *******************************/
/*************************** 拼接字符串 *******************************/
+(NSString *)makeAppendingString:(void (^)(RJBlockTool *))block{
if (block) {
RJBlockTool *tool = [[RJBlockTool alloc] init];
tool.resultString = @"date,who一起去看电影,备注:note";
block(tool);
return tool.resultString;
}
return @"为什么你什么都不说?";
}
-(RJBlockTool *(^)(NSString *))date{
return ^(NSString *str){
self.resultString = [self.resultString stringByReplacingOccurrencesOfString:@"date" withString:str];
return self;
};
}
-(RJBlockTool *(^)(NSString *))who{
return ^(NSString *str){
self.resultString = [self.resultString stringByReplacingOccurrencesOfString:@"who" withString:str];
return self;
};
}
-(RJBlockTool *(^)(NSString *))note{
return ^(NSString *str){
self.resultString = [self.resultString stringByReplacingOccurrencesOfString:@"note" withString:str];
return self;
};
}
/*************************** 拼接字符串 *******************************/
@end
二 使用
NSLog(@"计算结果1-->%f",[RJBlockTool makeCalculator:^(RJBlockTool *rj_tool) {
rj_tool.plus(10).subtract(2).multiply(5).divide(8);
}]);
NSLog(@"计算结果2-->%f",[RJBlockTool makeCalculator:^(RJBlockTool *rj_tool) {
rj_tool.plus(10);
rj_tool.subtract(2);
rj_tool.multiply(5);
rj_tool.divide(5);
}]);
NSLog(@"计算结果:-->%@",[RJBlockTool makeAppendingString:^(RJBlockTool *rj_tool) {
rj_tool.date(@"今天").who(@"我和她").note(@"嘿嘿嘿");
}]);
计算结果1-->5.000000
计算结果2-->8.000000
计算结果:-->今天,我和她一起去看电影,备注:嘿嘿嘿