这篇文章是参考了欧阳大哥的实现并在此基础上进行了一些扩展,原文链接在此一种查看Block中引用的所有外部对象的实现方法。想要详细了解如何解析block中引用的对象直接查看原文链接即可。
扩展说明:在原作者的实现中,没有实现打印__block类型的引用对象,我在此进行了补充。下图是打印出block中所有的引用对象

核心代码如下:
static NSString *block_description(id block, SEL _cmd) {
struct Block_layout *blockLayout = (__bridge struct Block_layout *)block;
struct Block_descriptor_1 *desc1 = blockLayout->descriptor;
struct Block_descriptor_2 *desc2 = NULL;
struct Block_descriptor_3 *desc3 = NULL;
NSMutableString *printStr = [NSMutableString new];
if (blockLayout->flags & BLOCK_HAS_COPY_DISPOSE) {
desc2 = (struct Block_descriptor_2 *)(desc1 + 1);
} else {
return printStr;
}
if (blockLayout->flags & BLOCK_HAS_EXTENDED_LAYOUT) {
desc3 = (struct Block_descriptor_3 *)(desc2 + 1);
} else {
return printStr;
}
if (desc3->layout == 0) {
return printStr;
}
const char *extendLayout = desc3->layout;
//如果layout小于0x1000,则将layout本身当成一个12bit的数据,此时是compact encoding
//压缩编码方式0xXYZ: X表示强引用数量,Y表示__block引用数量,Z表示弱引用数量,对该编码解码,从而统一下面的处理逻辑
if (extendLayout < (const char *)0x1000) {
char compactEncoding[4] = {0,0,0,0};
unsigned short xyz = (unsigned short)extendLayout;
unsigned char x = (xyz >> 8) & 0xf;
unsigned char y = (xyz >> 4) & 0xf;
unsigned char z = (xyz & 0xf);
int idx = 0;
if (x) {
x--;
compactEncoding[idx++] = (BLOCK_LAYOUT_STRONG << 4) | x;
}
if (y) {
y--;
compactEncoding[idx++] = (BLOCK_LAYOUT_BYREF << 4) | y;
}
if (z) {
z--;
compactEncoding[idx++] = (BLOCK_LAYOUT_WEAK << 4) | z;
}
extendLayout = compactEncoding;
}
//上面的代码解码了compact encoding情况
//下面进行layout的解析,block内部会优先把对象类型排在前面
//当layout大于0x1000,layout就是指向了一个字符串的指针
int index = 0;
int objOffest = sizeof(struct Block_layout);//block内部对象的偏移
char *typeArr[4] = {"strong ","byref ","weak ","unretain"};
char *byrefTypeArr[3] = {"__strong __block","__weak __block","__unsafe_unretained __block"};
while (extendLayout[index] != '\0') {
unsigned char PN = extendLayout[index];
unsigned char P = (PN >> 4) & 0xf; // 类型描述,strong,byref,weak等等
unsigned char N = (PN & 0xf) + 1; //对应类型的个数
//只针对对象类型进行判断
if (P >= BLOCK_LAYOUT_STRONG && P <= BLOCK_LAYOUT_UNRETAINED) {
int typeIndex = P - BLOCK_LAYOUT_STRONG;
if (P != BLOCK_LAYOUT_BYREF) {
[printStr appendFormat:@"\n引用类型:%s, 引用对象:",typeArr[typeIndex]];
for (int i = 0; i < N; i++) {
void *objPointer = *(void **)((void *)blockLayout + objOffest);
id obj = (__bridge id)(objPointer);
[printStr appendFormat:@"%@",obj];
if (i != N - 1) {
[printStr appendString:@","];
}
objOffest += sizeof(void *);//因为都是对象类型,每次偏移一个指针的大小
}
} else {
//额外处理__block封装的类型,严格来说byref不能算是对象类型,虽然它有isa,但是该值被设为了0
[printStr appendFormat:@"\n引用类型:%s, 引用对象:",typeArr[typeIndex]];
NSInteger byrefObjCount = 0;//计数byref封装obj的数量
for (int i = 0; i < N; i++) {
struct Block_byref *byref1 = *(struct Block_byref **)((void *)blockLayout + objOffest);
byref1 = byref1->forwarding;
if ((byref1->flags & BLOCK_BYREF_LAYOUT_MASK) != BLOCK_BYREF_LAYOUT_NON_OBJECT) {//说明是obj对象
int byrefTypeIndex = (((byref1->flags - BLOCK_BYREF_LAYOUT_STRONG) >> 28) & 0xf);
void *objPointer = *(void **)((void *)byref1 + byref1->size - sizeof(void *));
id obj = (__bridge id)(objPointer);
if (byrefObjCount > 0) {
[printStr appendString:@","];
}
[printStr appendFormat:@"%@(%s)",obj, byrefTypeArr[byrefTypeIndex]];
byrefObjCount++;
}
objOffest += sizeof(void *);//因为都是对象类型,每次偏移一个指针的大小
}
if (byrefObjCount == 0) {
[printStr appendString:@"无__block修饰的对象"];
}
}
}
index++;
}
return printStr;
}
完整代码链接BlockExtendLayout
网友评论