您好,登录后才能下订单哦!
本文小编为大家详细介绍“iOS block值捕获与指针捕获的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“iOS block值捕获与指针捕获的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
通俗的理解:
指针:内存地址
指针变量:存放内存地址的变量
指针变量的指针:指针变量自身的内存地址
Person *p = [Person new]
右边isa为:对象的内存地址 - 指针
p为:指针变量
左边isa为:指针变量的内存地址 - 指针变量的指针
对局部变量捕获有两种形式:1、值捕获(局部自动变量) 2、指针捕获(局部静态变量);全局变量无需捕获,可直接进行访问。
clang -rewrite-objc **.m -o **.cpp
不同场景下转换成C++代码结果如下(嫌代码长不想看的直接看代码下面的结论)
指针变量的捕获
block内部用一个新的指针变量来接收原指针变量。接收后,两个指针变量里面存储的值都是对象的内存地址,所以也可以说是值的捕获。
局部自动变量:
int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; void (^block)(void) = ^{ NSLog(@"%@",p); }; block(); } return 0; }
struct Person_IMPL { struct NSObject_IMPL NSObject_IVARS; }; struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0* Desc; Person *p; __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person *_p, int flags=0) : p(_p) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; } }; static void __main_block_func_0(struct __main_block_impl_0 *__cself) { Person *p = __cself->p; // bound by copy NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_f76e59_mi_0,p); } static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);} static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);} static struct __main_block_desc_0 { size_t reserved; size_t Block_size; void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*); void (*dispose)(struct __main_block_impl_0*); } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0}; int main(int argc, const char * argv[]) { /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; Person *p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new")); void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, p, 570425344)); ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block); } return 0; }
代码分析,生成的__main_block_impl_0结构体里面创建了一个指针变量p,main函数里面的__main_block_impl_0初始化时,传入的也是指针变量p。所以block对局部自动变量采用的捕获方式是指针变量的捕获,也就是值捕获。
对指针变量自身指针的捕获
block内部用一个新的指针来接收(指向)原指针变量自身的地址。
局部静态变量:
int main(int argc, const char * argv[]) { @autoreleasepool { static Person *p = nil; p = [Person new]; void (^block)(void) = ^{ NSLog(@"%@",p); }; block(); } return 0; }
struct Person_IMPL { struct NSObject_IMPL NSObject_IVARS; }; struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0* Desc; Person **p; __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, Person **_p, int flags=0) : p(_p) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; } }; static void __main_block_func_0(struct __main_block_impl_0 *__cself) { Person **p = __cself->p; // bound by copy NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_bd39c2_mi_0,(*p)); } static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);} static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);} static struct __main_block_desc_0 { size_t reserved; size_t Block_size; void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*); void (*dispose)(struct __main_block_impl_0*); } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0}; int main(int argc, const char * argv[]) { /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; static Person *p = __null; p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new")); void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &p, 570425344)); ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block); } return 0; }
代码分析,生成的__main_block_impl_0结构体里面创建了一个指针*p,main函数里面的__main_block_impl_0初始化时,传入的是指针变量p的地址&p。所以block对局部静态变量采用的捕获方式是指针变量自身地址的捕获,也就是指针捕获。
int main(int argc, const char * argv[]) { @autoreleasepool { __block Person *p = [Person new]; void (^block)(void) = ^{ NSLog(@"%@",p); }; block(); } return 0; }
struct Person_IMPL { struct NSObject_IMPL NSObject_IVARS; }; struct __Block_byref_p_0 { void *__isa; __Block_byref_p_0 *__forwarding; int __flags; int __size; void (*__Block_byref_id_object_copy)(void*, void*); void (*__Block_byref_id_object_dispose)(void*); Person *p; }; struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0* Desc; __Block_byref_p_0 *p; // by ref __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_p_0 *_p, int flags=0) : p(_p->__forwarding) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; } }; static void __main_block_func_0(struct __main_block_impl_0 *__cself) { __Block_byref_p_0 *p = __cself->p; // bound by ref NSLog((NSString *)&__NSConstantStringImpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_T_main_6c171f_mi_0,(p->__forwarding->p)); } static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 8/*BLOCK_FIELD_IS_BYREF*/);} static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 8/*BLOCK_FIELD_IS_BYREF*/);} static struct __main_block_desc_0 { size_t reserved; size_t Block_size; void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*); void (*dispose)(struct __main_block_impl_0*); } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0}; int main(int argc, const char * argv[]) { /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; __attribute__((__blocks__(byref))) __Block_byref_p_0 p = {(void*)0,(__Block_byref_p_0 *)&p, 33554432, sizeof(__Block_byref_p_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("new"))}; void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_p_0 *)&p, 570425344)); ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block); } return 0; }
代码分析,使用__block修饰的指针变量p,会被转换为__Block_byref_p_0的结构体,结构体内持有p。main函数里面初始化__main_block_impl_0时传入的是__Block_byref_p_0的地址,访问p时,通过__Block_byref_p_0的__forwarding指针进行访问。其实就相当于block内部捕获了__Block_byref_p_0的指针,通过指针去访问__Block_byref_p_0持有的p。所以__block修饰的变量本质上也相当于是一种指针捕获,只不过不是直接捕获指针变量p的自身地址。
值捕获能否重新赋值? 进行值拷贝时,block内部同名指针变量如果执行重新赋值操作,相当于使内部的指针变量指向了一个新的对象,再对此对象进行任何操作都与原指针变量指向的原对象无关,所以不能进行重新赋值。
指针捕获能否重新赋值? block内部将block外部的指针变量的指针赋值给一个新的指针,block内部、外部的指针都指向的是同一个指针变量。如果进行赋值操作,操作的是同一个指针变量,所以可以进行重新赋值。
读到这里,这篇“iOS block值捕获与指针捕获的方法”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。