美文网首页
__weak & __unsafe_unretained

__weak & __unsafe_unretained

作者: xiari1991 | 来源:发表于2018-05-16 11:47 被阅读15次

https://www.jianshu.com/p/df0269e0d3c3


//    __unsafe_unretained NSObject *boy1 = nil;
//    {
//        NSObject *boy2 = [[NSObject alloc] init];
//        boy1 = boy2;
//        NSLog(@"boy1 is %@", boy1);
//        NSLog(@"boy2 is %@", boy2);
//    }
//    NSLog(@"boy1 is %@", boy1);
//
    
    __weak NSObject *boy1 = nil;
    {
        NSObject *boy2 = [[NSObject alloc] init];
        boy1 = boy2;
        NSLog(@"boy1 is %@", boy1);
        NSLog(@"boy2 is %@", boy2);
    }
    NSLog(@"boy1 is %@", boy1);

__weak 在引用计数为0时,会指向nil。但时是有性能开销的。
__unsafe_unretained 在引用计数为0时,会指向原地址。只要访问该地址,就是野指针访问,会闪退。

相关文章

网友评论

      本文标题:__weak & __unsafe_unretained

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