1、Block的格式、使用
-
1、立即执行的block
^{ /*代码内容*/ }();
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//第一种写法:立马执行
^{
NSLog(@"this is a block");
}();
}
@end
-
2、无参数无返回值的Block
void(^Block变量名)(void) = ^(){ /*代码内容*/ };
void(^Block变量名)(void) = ^{ /*代码内容*/ };
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//第二种:先定义好这个无返回值无参数的block,在需要时代码调用
//现在的编辑器要求定义的时,形参列表的位置写void
void (^emptyBlock)(void) = ^{
NSLog(@"这是个无参数,无返回值的Block");
};
emptyBlock();
}
@end
-
3、有参数无返回值的Block
void(^Block变量名)(形参列表) = ^(形参列表){ /*代码内容*/ };
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//第三种:有参数无返回值的Block
void (^sumBlock)(int ,int ) = ^(int a,int b){
NSLog(@"%d + %d = %d",a,b,a+b);
};
//调用这个sumBlock的Block,得到的结果是20
sumBlock(10,10);
}
@end
-
4、有参数有返回值的Block
返回值类型 (^Block变量名)(形参列表) = ^(形参列表){ /*代码内容*/ };
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//第四种.有参数有返回值的Block
NSString* (^logBlock)(NSString *,NSString *) = ^(NSString *str1,NSString *str2){
return [NSString stringWithFormat:@"%@%@",str1,str2];
};
//调用logBlock
NSLog(@"%@", logBlock(@"我是",@"Block"));
}
@end
-
5、实际常用到的形式
结合typedef使用: 包含有返回值block 和无返回值block
在方法里作为方法入参使用
#import "ViewController.h"
typedef void(^AgeBlock)(int age1,int age2);
typedef NSString*(^NameBlock) (NSString *name1,NSString *name2);
@interface ViewController ()
@property(nonatomic,copy)AgeBlock ageBlock;
@property(nonatomic,copy)NameBlock nameBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.ageBlock = ^(int age1, int age2) {
int oldAge = age1>age2?age1:age2;
NSLog(@"输出较大的年龄:%d",oldAge);
};
self.nameBlock = ^NSString *(NSString *name1, NSString *name2) {
NSString *fullName = [NSString stringWithFormat:@"%@ %@",name1,name2];
NSLog(@"fullName is %@",fullName);
return fullName;
};
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//无返回值的block
self.ageBlock(12, 13);
//有返回值的block
NSString *myNameStr = self.nameBlock(@"zhang", @"san");
NSLog(@"myName is %@",myNameStr);
}
//block作为一个参数的情况
-(void)addWithBlock:(void(^)(int num))block{
}
@end
2、Block的本质
block本质上就是一个OC对象,是一个封装了函数调用以及函数调用环境的OC对象。
下面ViewController.m文件中有个引用了外部变量的block,我们可以通过clang 命令来观察其源码:
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc ViewController.m
可以找到对应block的源码:
- block底层源码结构
//block的底层是这样的一个结构体
struct __ViewController__touchesBegan_withEvent__block_impl_0 {
struct __block_impl impl;
struct __ViewController__touchesBegan_withEvent__block_desc_0* Desc;
int age; //这里可能还有block要用到的外部局部变量,根据具体block而定
//这是一个赋值过程,并不是block的结构
__ViewController__touchesBegan_withEvent__block_impl_0(void *fp, struct __ViewController__touchesBegan_withEvent__block_desc_0 *desc, int _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
//结构体的第一个成员
struct __block_impl {
void *isa; //是一个isa,即一个OC对象
int Flags;
int Reserved;
void *FuncPtr;//block的代码块内容,要执行的函数地址
};
//结构体的第二个成员
struct __ViewController__touchesBegan_withEvent__block_desc_0 {
size_t reserved;
size_t Block_size;//block的内存大小
} __ViewController__touchesBegan_withEvent__block_desc_0_DATA = { 0, sizeof(struct __ViewController__touchesBegan_withEvent__block_impl_0)};
3、总结
-
<1>block的原理是怎样的?本质是什么?
block的本质其实是一个封装了函数调用以及函数调用环境的OC对象。