传统的函数调用方式
Fouction *aFouction = [[Fouction alloc]init];
[aFouction dosomething];
[aFouction working];
Fouction.h
/**
传统写法
*/
-(void)dosomething;
-(void)working;
Fouction.m
-(void)dosomething{
NSLog(@"dosomething ");
}
-(void)working{
NSLog(@"working ");
}
链式编程调用
Fouction *aFouction = [[Fouction alloc]init];
[[[aFouction dosomething2] working2] dosomething2];
Fouction.h
/**
链式编程方法
@return 返回当前对象
*/
-(Fouction *)dosomething2;
-(Fouction *)working2;
Fouction.m
-(Fouction *)dosomething2{
//执行操作办法
NSLog(@"dosomething ");
return self;
}
-(Fouction *)working2{
//执行操作办法
NSLog(@"working");
return self;
}
函数式编程调用
Fouction *aFouction = [[Fouction alloc]init];
aFouction.dosomething4(@"工作").working4(@"休息");
Fouction.h
/**
函数式编程 无参数
*/
-(Fouction *(^)(void))dosomething3;
-(Fouction *(^)(void))working3;
/**
函数式编程 有参数
*/
-(Fouction *(^)(NSString *str))dosomething4;
-(Fouction *(^)(NSString * str))working4;
Fouction.m
-(Fouction *(^)(void))dosomething3{
Fouction *(^runBlock)(void) = ^ () {
NSLog(@"执行了不带参数的函数方法");
return self;
};
return runBlock;
// 改进写法
// return ^(){
// NSLog(@"Block Run2");
// return self;
// };
}
-(Fouction *(^)(void))working3 {
return ^() {
NSLog(@"执行了无参数的方法");
return self;
};
}
-(Fouction *(^)(NSString *))working4 {
Fouction *(^myBlock)(NSString *str) = ^(NSString *str) {
NSLog(@"执行了传值过来的参数方法--%@",str);
return self;
};
return myBlock;
// return ^(NSString *str) {
// NSLog(@"执行了传值过来的参数方法--%@",str);
// return self;
// };
}
-(Fouction *(^)(NSString *))dosomething4 {
return ^(NSString *str) {
NSLog(@"执行了传值过来的参数方法--%@",str);
return self;
};
}
源码git地址:https://gitee.com/WangBaofeng/hanshushibiancheng.git
网友评论