针对NSString的copy修饰前后,引用计数的变化
Posted 拳不离手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了针对NSString的copy修饰前后,引用计数的变化相关的知识,希望对你有一定的参考价值。
1 NSLog(@"NSString的对象,用NSString来接---------------------------------"); 2 NSString *string1 = [[NSString alloc]initWithFormat:@"不可变字符串"]; 3 NSLog(@"copy前,不可变字符串的引用计数 = %ld", [string1 retainCount]); 4 5 NSString *copyString1 = string1.copy; 6 NSLog(@"copy后,不可变字符串的引用计数 = %ld", [string1 retainCount]); 7 NSLog(@"copy后,copyString的引用计数 = %ld", [copyString1 retainCount]); 8 9 NSLog(@"%p", string1); 10 NSLog(@"%p", copyString1); 11 NSLog(@"NSMutableString的对象,用NSMutableString来接---------------------------------"); 12 NSMutableString *string2 = [[NSMutableString alloc]initWithFormat:@"可变字符串"]; 13 NSLog(@"copy前,可变字符串的引用计数 = %ld", [string2 retainCount]); 14 15 NSMutableString *copyString2 = string2.copy; 16 NSLog(@"copy后,可变字符串的引用计数 = %ld", [string2 retainCount]); 17 NSLog(@"copy后,copyString的引用计数 = %ld", [copyString2 retainCount]); 18 19 NSLog(@"%p", string2); 20 NSLog(@"%p", copyString2); 21 NSLog(@"NSString的对象,用NSMutableString来接---------------------------------"); 22 NSString *string3 = [[NSString alloc]initWithFormat:@"不可变字符串"]; 23 NSLog(@"copy前,不可变字符串的引用计数 = %ld", [string3 retainCount]); 24 25 NSMutableString *copyString3 = string3.copy; 26 NSLog(@"copy后,不可变字符串的引用计数 = %ld", [string3 retainCount]); 27 NSLog(@"copy后,copyString的引用计数 = %ld", [copyString3 retainCount]); 28 29 NSLog(@"%p", string3); 30 NSLog(@"%p", copyString3); 31 NSLog(@"NSMutableString的对象,用NSString来接---------------------------------"); 32 NSMutableString *string4 = [[NSMutableString alloc]initWithFormat:@"可变字符串"]; 33 NSLog(@"copy前,可变字符串的引用计数 = %ld", [string4 retainCount]); 34 35 NSString *copyString4 = string4.copy; 36 NSLog(@"copy后,可变字符串的引用计数 = %ld", [string4 retainCount]); 37 NSLog(@"copy后,copyString的引用计数 = %ld", [copyString4 retainCount]); 38 39 NSLog(@"%p", string4); 40 NSLog(@"%p", copyString4);
先来看看上面的运行结果,都打印出什么: 1 2016-06-15 11:39:06.411 OCLession9[1810:63941] NSString的对象,用NSString来接---------------------------------
2 2016-06-15 11:39:06.411 OCLession9[1810:63941] copy前,不可变字符串的引用计数 = 1 3 2016-06-15 11:39:06.411 OCLession9[1810:63941] copy后,不可变字符串的引用计数 = 2 4 2016-06-15 11:39:06.411 OCLession9[1810:63941] copy后,copyString的引用计数 = 2 5 2016-06-15 11:39:06.411 OCLession9[1810:63941] 0x100202740 6 2016-06-15 11:39:06.411 OCLession9[1810:63941] 0x100202740 7 2016-06-15 11:39:06.412 OCLession9[1810:63941] NSMutableString的对象,用NSMutableString来接--------------------------------- 8 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy前,可变字符串的引用计数 = 1 9 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy后,可变字符串的引用计数 = 1 10 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy后,copyString的引用计数 = 1 11 2016-06-15 11:39:06.412 OCLession9[1810:63941] 0x100300840 12 2016-06-15 11:39:06.412 OCLession9[1810:63941] 0x100300810 13 2016-06-15 11:39:06.412 OCLession9[1810:63941] NSString的对象,用NSMutableString来接--------------------------------- 14 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy前,不可变字符串的引用计数 = 1 15 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy后,不可变字符串的引用计数 = 2 16 2016-06-15 11:39:06.412 OCLession9[1810:63941] copy后,copyString的引用计数 = 2 17 2016-06-15 11:39:06.412 OCLession9[1810:63941] 0x100106f90 18 2016-06-15 11:39:06.412 OCLession9[1810:63941] 0x100106f90 19 2016-06-15 11:39:06.413 OCLession9[1810:63941] NSMutableString的对象,用NSString来接--------------------------------- 20 2016-06-15 11:39:06.413 OCLession9[1810:63941] copy前,可变字符串的引用计数 = 1 21 2016-06-15 11:39:06.413 OCLession9[1810:63941] copy后,可变字符串的引用计数 = 1 22 2016-06-15 11:39:06.413 OCLession9[1810:63941] copy后,copyString的引用计数 = 1 23 2016-06-15 11:39:06.413 OCLession9[1810:63941] 0x1006002f0
24 2016-06-15 11:39:06.413 OCLession9[1810:63941] 0x100600210
结论:对字符串,只有对不可变字符串的不可变拷贝才是浅拷贝,其余的都是深拷贝。
以上是关于针对NSString的copy修饰前后,引用计数的变化的主要内容,如果未能解决你的问题,请参考以下文章
iOS OC中strong,weak,assign,copy等的区别