zoukankan      html  css  js  c++  java
  • 针对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

    结论:对字符串,只有对不可变字符串的不可变拷贝才是浅拷贝,其余的都是深拷贝。

  • 相关阅读:
    JavaScript内部原理:浏览器的内幕
    DOM结构深度优先遍历(二):TreeWalker
    DOM结构深度优先遍历(一):NodeIterator
    Linux bc 命令
    两个小function实现XML和string相互转化
    Git 多用户配置
    Vue 新增的$attrs与$listeners的详解
    CSS mask遮罩
    flask之后端传值给前端
    python cv2在验证码识别中的使用
  • 原文地址:https://www.cnblogs.com/billios/p/5587037.html
Copyright © 2011-2022 走看看