tips:-rewrite-objc 选项将源代码编译为C++源代码
首先是c语言源文件中实现block:
#include <stdio.h>
int main(int argc, const char * argv[])
{
void(^blk)(void) = ^{
printf("Block\n");
};
blk();
return 0;
}
编译之后:
//block结构体 (__block_impl的实例)
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
//构造函数
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
//block变量 带有自动变量的匿名函数 (匿名函数被当作简单c函数)
//struct __main_block_impl_0 *__csel 指向block变量的指针
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
printf("Block\n");
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, const char * argv[])
{
//声明block
//struct _main_block_impl_0 tmp = __main_block_impl_0(__main_block_func_0,&__mian_block_desc_0_DATA)
//struct _main_block_impl_0 * blk = & tmp;
void(*blk)(void) = (void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA);
//执行block
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
return 0;
}
结论:Block == oc 对象。