//定义一个函数,传入block类型参数myBlock5
int fun(int (^myBlock5)(int a,int b))
{
return myBlock5(10,20);
}
int (^myBloak2)(int,int);
myBloak2 = ^(int a, int b){
return a+b;
};
//函数调用部分
NSLog(@"%d",fun(myBloak2));//以myBloak5传入的形参,myBloak2的实现方法进行计算
//内联 block格式
fun(^int(int a, int b) {
return a+b;
});
NSLog(@"%d", fun(^int(int a, int b) {
return a+b;
}));
//
typedef NSString* (^Myblock1)(NSString *a);//定义代码块类型
@interface Student : NSObject
-(NSString *)myBlock2:(NSString *)string andBlock:(Myblock1)block2;//方法调用代码块
@end
@implementation Student
-(NSString *)myBlock2:(NSString *)string andBlock:(Myblock1)block2
{
NSLog(@"调用代码块%@",string);//输出:调用代码块liuguan
return block2(string);//方法返回代码块的返回值
}
@end
实现部分:
Student *student = [[Student alloc] init];
NSLog(@"%@", [student myBlock2:@"liuguan" andBlock:^NSString *(NSString *a)
{
NSLog(@"我是%@",a);//输出:我是liuguan因为返回block2(string)
NSString *b = [@"123" stringByAppendingString:a];
return b;//输出:123liuguan
}
]
);