美文网首页
2018-08-06

2018-08-06

作者: Gradlyarn | 来源:发表于2018-08-06 11:23 被阅读12次
    //weak-strong dance
    Person *p1 = [[Person alloc] init];
    __weak Person *wp1 = p1;
    p1.myBlock = ^(){
          __strong Person *sp1 = wp1;
          NSLog(@"execute block");
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             NSLog(@"%@",sp1);
            });
        };
    p1.myBlock();
    
    //结果:
    2018-08-06 23:08:03.315110+0800 Demo[43110:8392352] execute block
    2018-08-06 23:08:04.315584+0800 Demo[43110:8392352] <Person: 0x60000000a960>
    2018-08-06 23:08:04.315797+0800 Demo[43110:8392352] -[Person dealloc]
    
    //weak
    Person *p1 = [[Person alloc] init];
     __weak Person *wp1 = p1;
     p1.myBlock = ^(){
          NSLog(@"execute block");
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               NSLog(@"%@",wp1);
            });
        };
    p1.myBlock();
    
    //结果:
    2018-08-06 23:09:11.973212+0800 Demo[43138:8394024] execute block
    2018-08-06 23:09:11.973464+0800 Demo[43138:8394024] -[Person dealloc]
    2018-08-06 23:09:13.062905+0800 Demo[43138:8394024] (null)
    

    相关文章

      网友评论

          本文标题:2018-08-06

          本文链接:https://www.haomeiwen.com/subject/wqmwvftx.html