1.第一种方法
声明block:
- (void)test:(int) param_1 completion:(void(^)(int)) completion;
实现block:
-(void)test:(int)param_1 completion:(void (^)(int))completion {
int total = ++param_1;
completion(total); //这个一定要写
}
调用block:
[self test: completion:^(int param_2) {
NSLog(@"param_2:%d",param_2);
}];
解释一下上面的方法,调用block的时候传递的是10,10在"实现block"那一步被加了1,Log打印出来的就是11
2.第二种方法
声明block:
声明:(返回类型)(^声明的block名称)(参数列表);
typedef void(^completion)(NSString *param_1, NSString *param_2);
构造含有block的方法
- (void)testNSString:(NSString *)param_1 completion:(completion) completion;
实现block
-(void)testNSString:(NSString *)param_1 completion:(completion)completion {
NSString *test_1 = [param_1 stringByAppendingString:@" world"];
NSString *test_2 = @"!";
completion(test_1, test_2);//这个一定要写
}
调用block
[self testNSString:@"hello" completion:^(NSString *param_1, NSString *param_2) {
NSLog(@"输出内容是:%@",[param_1 stringByAppendingString:param_2]);
}];
解释一下上面的方法,调用block的时候传递的是"hello","hello"在"实现block"那一步被加了" world",Log打印出来的就是"输出内容是:hello world!".