Ios自己面试总结

Block

2015-12-24  本文已影响913人  张xd
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        void(^block)() = ^{
            NSLog(@"%d",a);
        };
        
        a = 20;
        block();
        
    }
    return 0;
}
//编译后如下:(clang -rewrite-objc  main.m)

struct __main_block_impl_0 {
  struct __block_impl impl;        //__block_impl  isa,Flags,FunPtr
  struct __main_block_desc_0* Desc;
  int a;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  int a = __cself->a; // bound by copy

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_gw_h9qp96015ds8c2bdsbg65x880000gn_T_main_bb56b8_mi_0,a);
        }

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[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 

        int a = 10;
        void(*block)() = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
  
//void(*block)()  函数指针
//(void (*)()  进行类型转换
//__main_block_impl_0   是一个函数,有三个参数,  可以搜一下它是一个结构体,c++中的结构体相当于OC的类
//__main_block_func_0   block{}内部的实现  也是一个函数
//a 
        a = 20;
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

    }
    return 0;
}```



int main(int argc, const char * argv[]) {
@autoreleasepool {

    __block int a = 10;
    void(^block)() = ^{
        NSLog(@"%d",a);
    };
    
    a = 20;
    block();
    
}
return 0;

}
//编译后如下:

struct __Block_byref_a_0 {
void *__isa;
__Block_byref_a_0 *__forwarding;
int __flags;
int __size;
int a;
};

struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_a_0 *a; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_a_0 *_a, int flags=0) : a(_a->__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_a_0 *a = __cself->a; // bound by ref

        NSLog((NSString *)&__NSConstantStringImpl__var_folders_gw_h9qp96015ds8c2bdsbg65x880000gn_T_main_5caff8_mi_0,(a->__forwarding->a));
    }

static void __main_block_copy_0(struct __main_block_impl_0dst, struct __main_block_impl_0src) {_Block_object_assign((void)&dst->a, (void)src->a, 8/BLOCK_FIELD_IS_BYREF/);}

static void __main_block_dispose_0(struct __main_block_impl_0src) {_Block_object_dispose((void)src->a, 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_a_0 a = {(void*)0,(__Block_byref_a_0 *)&a, 0, sizeof(__Block_byref_a_0), 10};

//相比没有__block 的多出一个__Block_byref_a_0
void(block)() = ((void ()())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_a_0 *)&a, 570425344));

//传值有一处不同 (__Block_byref_a_0 *)&a 传的是结构体的地址

    (a.__forwarding->a) = 20;

//(a.__forwarding->a) 通过地址修改a的值
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

}
return 0;

}```

上一篇下一篇

猜你喜欢

热点阅读