欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Weak

程序员文章站 2024-01-14 22:28:40
...

示例代码:

__weak NSObject *object;

{
    NSObject *object_1 = [NSObject new];
    NSLog(@"Retain Count = %ld", CFGetRetainCount((__bridge CFTypeRef)(object_1)));
    object = object_1;
    NSLog(@"Retain Count = %ld", CFGetRetainCount((__bridge CFTypeRef)(object_1)));
}

运行结果:

Retain Count = 1
Retain Count = 1

示例代码:

__strong NSObject *object;

{
    NSObject *object_1 = [NSObject new];
    NSLog(@"Retain Count = %ld", CFGetRetainCount((__bridge CFTypeRef)(object_1)));
    object = object_1;
    NSLog(@"Retain Count = %ld", CFGetRetainCount((__bridge CFTypeRef)(object_1)));
}

运行结果:

Retain Count = 1
Retain Count = 2

从上面的代码可以看出:

  • weak变量无法使对象的计数器+1
相关标签: iOS weak