码迷,mamicode.com
首页 > 移动开发 > 详细

iOS block的变量捕获(capture)

时间:2020-02-17 00:49:59      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:pos   函数   stat   color   res   amp   alt   span   http   

技术图片

变量捕获会在blcok结构体内部生成一个变量已来存储捕获到的指针或者值

#import "Person.h"

@implementation Person
/*
 oc每个函数后面都自带 (Person * self, SEL _cmd)
 -(void)test:(Person * self, SEL _cmd);
 而入参都是局部变量 所以下面代码会捕获 self 
 */
-(void)test{
    void (^bblock)(void)=^{
        NSLog(@"%@",self);
    };
    bblock();
}
/*
 struct __Person__test_block_impl_0 {
   struct __block_impl impl;
   struct __Person__test_block_desc_0* Desc;
   Person *self;
   __Person__test_block_impl_0(void *fp, struct __Person__test_block_desc_0 *desc, Person *_self, int flags=0) : self(_self) {
     impl.isa = &_NSConcreteStackBlock;
     impl.Flags = flags;
     impl.FuncPtr = fp;
     Desc = desc;
   }
 };
 
 static struct __Person__test_block_desc_0 {
   size_t reserved;
   size_t Block_size;
   void (*copy)(struct __Person__test_block_impl_0*, struct __Person__test_block_impl_0*);
   void (*dispose)(struct __Person__test_block_impl_0*);
 } __Person__test_block_desc_0_DATA = { 0, sizeof(struct __Person__test_block_impl_0), __Person__test_block_copy_0, __Person__test_block_dispose_0};

 static void _I_Person_test(Person * self, SEL _cmd) {
     void (*bblock)(void)=((void (*)())&__Person__test_block_impl_0((void *)__Person__test_block_func_0, &__Person__test_block_desc_0_DATA, self, 570425344));
     ((void (*)(__block_impl *))((__block_impl *)bblock)->FuncPtr)((__block_impl *)bblock);
 }
 
 */
@end

 

iOS block的变量捕获(capture)

标签:pos   函数   stat   color   res   amp   alt   span   http   

原文地址:https://www.cnblogs.com/ZhangShengjie/p/12319422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!