这是我在文件 main.m 中的源代码
__block NSInteger blockInteger = 123;
static NSInteger staticInteger = 123;
void (^testBlock)(void) = ^() {
blockInteger++;
staticInteger++;
NSLog(@"%ld", blockInteger);
NSLog(@"%ld", staticInteger);
};
testBlock();
当我使用 clang 命令“clang -rewrite-objc main.m”时,我得到了这个
struct __Block_byref_blockInteger_0 {
void *__isa;
__Block_byref_blockInteger_0 *__forwarding;
int __flags;
int __size;
NSInteger blockInteger;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger *staticInteger;
__Block_byref_blockInteger_0 *blockInteger; // by ref
...
};
我想知道为什么 block 使用 __Block_byref_blockInteger_0 来捕获 blockInteger 因为它使用 NSInteger 指针来捕获静态变量。__Block_byref_blockInteger_0 究竟做了什么?通过与指针比较,这个结构有什么优势?