链式语法小试牛刀

ViewController.h文件

#import 
@interface ViewController : UIViewController
@property (nonatomic, assign) int result;
- (ViewController *(^)(int value))add;
+ (int)manager:(void(^)(ViewController *vc))block;
@end

ViewController.m文件

#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
+ (int)manager:(void(^)(ViewController *vc))block{
  ViewController *vc = [ViewController new];
  block(vc);
  return vc.result;
}
- (ViewController *(^)(int value))add{
  return ^ViewController *(int value){
    _result += value;
    return self;
  }
}
- (void)viewDidLoad{
  [super viewDidLoad];
  int result = [ViewController manager:^(ViewController *vc){
    vc.add(1).add(12);
  }];
  //result 为13
  //vc.add返回的是一个block,这个vc.add相当于在调用返回的这个block,需要一个int参数,返回值是ViewController对象本身,然后再.add,重复之前的动作
  
}
@end

你可能感兴趣的:(链式语法小试牛刀)