• 傻瓜学编程之block_3


    block 捕获自动变量的瞬间值;

    注释在代码中;请参考傻瓜学编程之block_3

    import "People.h"
    @interface People()
    {
        int yourNameis;
       
        
    }
    @end
    
    
    @implementation People
    -(instancetype)init{
        if ( [super init]) {
            [self myBlcokTest];
        }
        return self;
        
        
        
    }
    
    - (void)myBlcokTest{
       int ddkknn = 555555;
        char bbb = '3';
        const char *fmt = &bbb;
        void (^blk)()=^{
            printf("%d",ddkknn);
            printf("%s",fmt);
    
        };
        blk();
        
      
    }
    // 变量直接添加 属性生成_xxxx;
    struct People_IMPL {
        struct NSObject_IMPL NSObject_IVARS;
        int yourNameis;
        NSString *_thisIsPro;
    };
    
    
    /**
     1、结构体内部生成的成员变量与声明的成员变量的类型相同;
     2、block语法中没有使用的自动变量不会被追加,只有在block语法中使用的自动变量才会被追加;
     3、根据构造函数传递过来的参数对追加的成员变量进行初始化;
     */
    struct __People__myBlcokTest_block_impl_0 {
        struct __block_impl impl;
        struct __People__myBlcokTest_block_desc_0* Desc;
        int ddkknn;
        const char *fmt;
        __People__myBlcokTest_block_impl_0(void *fp, struct __People__myBlcokTest_block_desc_0 *desc, int _ddkknn, const char *_fmt, int flags=0) : ddkknn(_ddkknn), fmt(_fmt) {
            impl.isa = &_NSConcreteStackBlock;
            impl.Flags = flags;
            impl.FuncPtr = fp;
            Desc = desc;
        }
        
    //   相当于:
        __People__myBlcokTest_block_impl_0(void *fp, struct __People__myBlcokTest_block_desc_0 *desc, int _ddkknn, const char *_fmt, int flags=0) : ddkknn(_ddkknn), fmt(_fmt) {
            impl.isa = &_NSConcreteStackBlock;
            impl.Flags = flags;
            impl.FuncPtr = fp;
            Desc = desc;
            ddkknn = 5555;
            fmt = '3';
        }
    };
    
    
    //此时使用的是保存在__People__myBlcokTest_block_impl_0结构体实例的成员变量中那些自动变量的值 即(int ddkknn;
    //const char *fmt;) 也就是截获自动变量 就是在执行block时,block所使用的自动变量保存在block 的结构体实例中;
    static void __People__myBlcokTest_block_func_0(struct __People__myBlcokTest_block_impl_0 *__cself) {
        int ddkknn = __cself->ddkknn; // bound by copy
        const char *fmt = __cself->fmt; // bound by copy
        
        printf("%d",ddkknn);
        printf("%s",fmt);
        
    }
    
    static struct __People__myBlcokTest_block_desc_0 {
        size_t reserved;
        size_t Block_size;
    } __People__myBlcokTest_block_desc_0_DATA = { 0, sizeof(struct __People__myBlcokTest_block_impl_0)};
    
    static void _I_People_myBlcokTest(People * self, SEL _cmd) {
        int ddkknn = 555555;
        char bbb = '3';
        const char *fmt = &bbb;
        void (*blk)()=((void (*)())&__People__myBlcokTest_block_impl_0((void *)__People__myBlcokTest_block_func_0, &__People__myBlcokTest_block_desc_0_DATA, ddkknn, fmt));
        ((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
        
        
    }
    
  • 相关阅读:
    Teamwork——Week4 团队分工和预估项目时间
    Team Homework #3 软件工程在北航——IloveSE
    《DWZ笔记一》<select>动态联动菜单
    asp.net下使用jquery 的ajax+WebService+json 实现无刷新取后台值的实现
    经典SQL语句基础50题
    打垮美国发起的这场经济战争[转自由凤凰论坛]
    为什么要删我贴呢?
    5年,从5元到500万,我的创业经历(转自VFP精英站,未完)
    发现大家对销售感兴趣,再来两则(二)
    你们知道美国是怎么报道别人的灾难吗?
  • 原文地址:https://www.cnblogs.com/xiaowuqing/p/7648365.html
Copyright © 2020-2023  润新知