带__Block和不带的差别

带了__block

#pragma clang assume_nonnull end

struct __Block_byref_i_0 {
  void *__isa;
__Block_byref_i_0 *__forwarding;
 int __flags;
 int __size;
 int i;
};

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;
  }
};
static int __main_block_func_0(struct __main_block_impl_0 *__cself, int index) {

        index += 1;
        printf("====> %d\n",index);
        return index;
    }

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 () {
    __Block_byref_i_0 i = {(void*)0,(__Block_byref_i_0 *)&i, 0, sizeof(__Block_byref_i_0), 10};
    int (*testBlk)(int) = ((int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
    printf("---> %d\n",((int (*)(__block_impl *, int))((__block_impl *)testBlk)->FuncPtr)((__block_impl *)testBlk, (i.__forwarding->i)));
    return 0;
}
#import <Foundation/Foundation.h>

int main () {
    int __block i = 10;
    int (^testBlk)(int) = ^int(int index){
        index += 1;
        printf("====> %d\n",index);
        return index;
    };
    printf("---> %d\n",testBlk(i));
    return 0;
}

 

没有带__block

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;
  }
};
static int __main_block_func_0(struct __main_block_impl_0 *__cself, int index) {

        int addIndex = index + 1;
        printf("====> %d\n",addIndex);
        return addIndex;
    }

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 i = 10;
    int (*testBlk)(int) = ((int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
    printf("---> %d\n",((int (*)(__block_impl *, int))((__block_impl *)testBlk)->FuncPtr)((__block_impl *)testBlk, i));
    return 0;
}
#import <Foundation/Foundation.h>

int main () {
    int i = 10;
    int (^testBlk)(int) = ^int(int index){
        int addIndex = index + 1;
        printf("====> %d\n",addIndex);
        return addIndex;
    };
    printf("---> %d\n",testBlk(i));
    return 0;
}

 

posted @ 2019-08-01 14:24  雨筱逸悠  阅读(233)  评论(0编辑  收藏  举报