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
推荐阅读
-
Weak
-
evpp源码编译time_weak错误
-
IOS开发中的属性readwrite,readonly,retain,assign,copy,nonatomic,strong,weak.
-
IOS中(assign,retain,copy,weak,strong)的区别以及nonatomic的含义
-
iOS 中weak的实现代码示例
-
iOS 中weak的实现代码示例
-
智能指针之 weak_ptr
-
c/c++ 智能指针 weak_ptr 使用
-
IOS开发(77)之iOS高级内存管理:比较__unsafe_unretain、__strong、__weak、__autoreleasing
-
IOS开发(87)之Strong与Weak的理解