从《block-底层数据结构》中,我们知道block的本质就是就是OC对象,他也有isa指针,它既然是对象,那它是属于什么类型呢?那这章节,我们来探讨下block的类型
block有三种类型,可以通过调用class方法或者isa指针查看具体类型,最终都是继承NSBlock类型。
NSGlobalBlock (__NSConcreteGlobalBlock)
NSStackBlock (__NSConcreteStackBlock)
NSMallocBlock (__NSConcreteMallocBlock)
先来简单的看下下面的代码,通过代码分析block的类型。
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
void (^block)(void) = ^() {
NSLog(@"Hello, World!");
};
NSLog(@"%@",[block class]);
NSLog(@"%@",[[block class] superclass]);
NSLog(@"%@",[[[block class] superclass] superclass]);
NSLog(@"%@",[[[[block class] superclass] superclass] superclass]);
NSLog(@"%@",[[[[[block class] superclass] superclass] superclass] superclass]);
}
return 0;
}
// 控制台打印
2018-07-02 14:55:39.799788+0800 block的类型[17627:1408539] __NSGlobalBlock__
2018-07-02 14:55:39.799954+0800 block的类型[17627:1408539] __NSGlobalBlock
2018-07-02 14:55:39.799970+0800 block的类型[17627:1408539] NSBlock
2018-07-02 14:55:39.799984+0800 block的类型[17627:1408539] NSObject
2018-07-02 14:55:39.799993+0800 block的类型[17627:1408539] (null)
Program ended with exit code: 0
从控制台的打印,可以得出的结论__NSGlobalBlock__--> __NSGlobalBlock --> NSBlock--> NSObject
,最终block也是继承于NSObject。(结论一:block继承于NSObject)
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// Global:没有访问auto变量
void (^block1)(void) = ^() {
NSLog(@"Hello, World!");
};
// Stack:访问了auto变量
auto int age = 10;
void (^block2)(void) = ^(){
NSLog(@"%d",age);
};
NSLog(@"%@,%@,%@",[block1 superclass],[block2 superclass],[^{
NSLog(@"%d",age);
} class]);
}
return 0;
}
// 控制台打印
2018-07-02 15:06:59.997872+0800 block的类型[17735:1426379] __NSGlobalBlock, __NSMallocBlock, __NSStackBlock__
Program ended with exit code: 0
从上面代码的打印的三个block中,可以看出block有三种,分别是__NSGlobalBlock, __NSMallocBlock, __NSStackBlock_
。(结论二:block的三种类型__NSGlobalBlock, __NSMallocBlock, __NSStackBlock_)
那我们如何区分block到底是NSGlobalBlock
、NSStackBlock
还是NSMallocBlock
呢?下面我们来看再来看下面的代码,我举些例子让大家知道如何区分block的类型。
NSGlobalBlock
int a = 20;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// NSGlobalBlock:没有访问auto变量
void (^block1)(void) = ^ {
NSLog(@"Hello");
};
NSLog(@"%@",[block1 class]);
static int age = 10;
void (^block2)(void) = ^ {
NSLog(@"age is %d",age);
};
NSLog(@"%@",[block2 class]);
void (^block3)(void) = ^ {
NSLog(@"a is %d",a);
};
NSLog(@"%@",[block3 class]);
}
return 0;
}
// 控制台打印
2018-07-02 15:24:40.575459+0800 block的类型2[17895:1449262] __NSGlobalBlock__
2018-07-02 15:24:40.575640+0800 block的类型2[17895:1449262] __NSGlobalBlock__
2018-07-02 15:24:40.575651+0800 block的类型2[17895:1449262] __NSGlobalBlock__
Program ended with exit code: 0
来分析一下上面的代码,block1、block2、block3都没有访问auto变量,blcok1直接打印“Hello”
,block2访问了静态变量age
,block3访问的是全局变量a
,他们三者的类型都是NSGlobalBlock类型,所以可以得知(结论三:①NSGlobalBlock:没有访问auto变量)
NSStackBlock
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// NSStackBlock:访问auto变量
auto int age = 10;
void (^block)(void) = ^ {
NSLog(@"age is %d",age);
};
NSLog(@"%@",[block class]);
}
return 0;
}
//控制台打印
// ARC环境下打印的结果
2018-07-02 16:03:21.152819+0800 block的类型2[18168:1493595] __NSMallocBlock__
Program ended with exit code: 0
// MRC环境下打印的结果
2018-07-02 16:08:01.000861+0800 block的类型2[18204:1499788] __NSStackBlock__
Program ended with exit code: 0
从上面代码的控制台打印输出的block类型是NSMallockBlock
,为什么是NSMallockBlock
?因为这是在ARC(自动管理内存)环境下,苹果自动帮我们将block做了复制操作,这个我们后面的章节再讨论,所以这里我们要把ARC调成MRC的环境下,再打印,NSStackBlock
才是block真实类型。(结论三:②NSStackBlock:访问了auto变量)
NSMallocBlock
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// NSMallocBlock:NSStackBlock copy后就是NSMallocBlock,NSMallocBlock是存在堆空间
auto int age = 10;
void (^block)(void) = [^ {
NSLog(@"age is %d",age);
} copy];
NSLog(@"%@",[block class]);
}
return 0;
}
//控制台打印
// MRC环境下打印的结果
2018-07-02 16:08:01.000861+0800 block的类型2[18204:1499788] __NSMallocBlock__
Program ended with exit code: 0
NSStackBlock
copy后就是NSMallocBlock
,NSMallocBlock
是存在堆空间的。
(结论三:③NSMallocBlock:NSStackBlock调用了copy)
再来看看一下两张图。
每一种类型的block调用copy后的结果如下所示
总结:综合上面的分析我们得出三个结论
结论一:block继承于NSObject
结论二:block的三种类型__NSGlobalBlock, __NSMallocBlock, __NSStackBlock_
结论三:
①NSGlobalBlock:没有访问auto变量
②NSStackBlock:访问了auto变量
③NSMallocBlock:NSStackBlock调用了copy