创建 mrc 工程
- 正常创建一个项目
- 选中工程文件
- 选中TARGETS中选中需要更改的项目
- 选中bulidSeting
- 在搜索框中搜索“auto”
- 将Automatic Reference Counting 将YES改为NO
mrc工程.png
字符串 或 基本数据类型
// 数组在初始化的时候 retainCount 为 1
// retain 后 retainCount + 1
NSMutableArray *arr = [[NSMutableArray array] retain];
NSString *str = [NSString stringWithFormat:@"abc "];
[str retain];
[arr addObject:str];
// 输出为 18446744073709551615 即 (2^64 - 1)
NSLog(@"%@%lu", str,[str retainCount]);
// 输出为 -1
NSLog(@"%@%d", str, [str retainCount]);
// 输出为2
NSLog(@"###%d",[arr retainCount]);
[str retain];
[str release];
[str release];
// 输出为-1
NSLog(@"%@%d", str, [str retainCount]);
[arr removeAllObjects];
// 输出为 -1
NSLog(@"%@%d", str, [str retainCount]);
// 输出为 2
NSLog(@"33333 %d",[arr retainCount]);
结果1.png
对象
NSMutableArray *arr = [[NSMutableArray array] retain];
// 对象 初始化 retainCount 为 1
NSObject *obj = [[NSObject alloc] init];
// retainCount + 1
[obj retain];
// retainCount + 1
[arr addObject:obj];
// 3
NSLog(@"%lu" ,[obj retainCount]);
// 3
NSLog(@"%d", [obj retainCount]);
// 2
NSLog(@"###%d",[arr retainCount]);
// retainCount + 1
[obj retain];
// retainCount - 1
[obj release];
// retainCount - 1
[obj release];
// 2
NSLog(@"%d", [obj retainCount]);
// retainCount - 1
[arr removeAllObjects];
// 1
NSLog(@"%d", [obj retainCount]);
// 2
NSLog(@"33333 %d",[arr retainCount]);
结果2.png
网友评论