//声明块
返回类型 (^块名称)(参数列表)
void (^blockA)(void) = 0;
//定义块
void (^blockB)(int) = ^(int b){
NSLog(@"block B, b=%d", b);
};
//用参数块实现回调
+(void) blockParam:(int)a b:(int(^)(int))b{
NSLog(@"a = %d", a);
if (b) {
NSLog(@"call b block");
b(100);
}
}
+(void) test{
[self blockParam:99 b:^(int p){
NSLog(@"this is b:%d", p);
return p;
}];
}
本文详细介绍了 Objective-C 中块(Block)的基本概念、声明与定义方式,并通过实例展示了如何利用块实现回调功能,加深了开发者对于块在实际编程中应用的理解。

2万+

被折叠的 条评论
为什么被折叠?



