本文整理下Block含参和含返回值和函数作为参数传递的情况,写下笔记方便复查
@implementation ViewController
static int n = 99;
static NSString *name = @"test";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
void(^block0)(void) = ^{
NSLog(@"-----block0");
};
block0();
NSLog(@"block0 => %@",block0);
void(^aBlock)(NSString *x ,NSString *y);
aBlock = ^(NSString *x ,NSString *y){
NSLog(@"----%@---%@",x,y);
};
aBlock(@"lilei",@"hanmeimei");
int(^addBlock)(int ,int) = ^(int x, int y){
return x +y;
};
[self useBlockForOC:addBlock];
[self useBlockForOC:^(int x, int y) {
return x+y;
}];
}
-(void)useBlockForOC:(int(^)(int ,int))aBlock{
NSLog(@"result = %d" ,aBlock(200 ,300));
}
@end
网友评论