創(chuàng)建 mrc 工程
- 正常創(chuàng)建一個(gè)項(xiàng)目
- 選中工程文件
- 選中TARGETS中選中需要更改的項(xiàng)目
- 選中bulidSeting
- 在搜索框中搜索“auto”
- 將Automatic Reference Counting 將YES改為NO
字符串 或 基本數(shù)據(jù)類型
// 數(shù)組在初始化的時(shí)候 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]);
對(duì)象
NSMutableArray *arr = [[NSMutableArray array] retain];
// 對(duì)象 初始化 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]);