美文网首页
NSString copy strong

NSString copy strong

作者: 愤怒小鸟飞呀飞 | 来源:发表于2018-07-04 13:05 被阅读0次

    从本类及其子类两方面分析:可创建可变对象和不可变对象两种

       NSString *str = @"1234";
        NSString *str2 = [str copy];
        NSString *str3 = str;
        
        NSLog(@"str地址:%p,str:%@",str,str);
        NSLog(@"str2地址:%p,str2:%@",str2,str2);
        NSLog(@"str3地址:%p,str3:%@",str3,str3);
        
    //    str2 = @"22222";
        str = @"new 1234";
        NSLog(@"changing\n ...");
        
        NSLog(@"str地址:%p,str:%@",str,str);
        NSLog(@"str2地址:%p,str2:%@",str2,str2);
        NSLog(@"str3地址:%p,str3:%@",str3,str3);
    
    打印结果:
    2018-07-04 17:30:25.652461+0800 Test[93406:1493493] str地址:0x104e80aa8,str:1234
    2018-07-04 17:30:25.652610+0800 Test[93406:1493493] str2地址:0x104e80aa8,str2:1234
    2018-07-04 17:30:25.652762+0800 Test[93406:1493493] str3地址:0x104e80aa8,str3:1234
    2018-07-04 17:30:25.652870+0800 Test[93406:1493493] changing
     ...
    2018-07-04 17:30:25.653056+0800 Test[93406:1493493] str地址:0x104e80b28,str:new 1234
    2018-07-04 17:30:25.653236+0800 Test[93406:1493493] str2地址:0x104e80aa8,str2:1234
    2018-07-04 17:30:25.653353+0800 Test[93406:1493493] str3地址:0x104e80aa8,str3:1234
    

    以上结果可见:不可变字符对象,strong copy均不开辟新的内存空间,为浅拷贝

    这是其实是事实意义上的深拷贝.因为无论你怎么修改str的值,都不会影响到str3.---bySpring (略有争议)

       NSString  *str = @"a";
        NSString *str2 = @"a";
        
        NSLog(@"str地址:%p,str:%@",str,str);
        NSLog(@"str2地址:%p,str2:%@",str2,str2);
    
    2018-07-04 17:36:22.216211+0800 Test[93558:1499765] str地址:0x10f1a1aa8,str:a
    2018-07-04 17:36:22.216344+0800 Test[93558:1499765] str2地址:0x10f1a1aa8,str2:a
    

    相同字符串在内存中开辟的空间是一样的。对于NSString的不可变对象,copy同strong

    NSMutableString *str = [NSMutableString stringWithFormat:@"222"];
        NSString *str2 = [str copy];
        NSString *str3 = str;
        
        NSLog(@"str地址:%p,str:%@",str,str);
        NSLog(@"str2地址:%p,str2:%@",str2,str2);
        NSLog(@"str3地址:%p,str3:%@",str3,str3);
        
        [str replaceCharactersInRange:NSMakeRange(0, 1) withString:@"new--"];
        NSLog(@"changing\n ...");
        
        NSLog(@"str地址:%p,str:%@",str,str);
        NSLog(@"str2地址:%p,str2:%@",str2,str2);
        NSLog(@"str3地址:%p,str3:%@",str3,str3);
    
    打印结果
    2018-07-04 17:42:02.876714+0800 Test[93703:1505747] str地址:0x604000448700,str:222
    2018-07-04 17:42:02.876903+0800 Test[93703:1505747] str2地址:0xa000000003232323,str2:222
    2018-07-04 17:42:02.877027+0800 Test[93703:1505747] str3地址:0x604000448700,str3:222
    2018-07-04 17:42:02.877156+0800 Test[93703:1505747] changing
     ...
    2018-07-04 17:42:02.877332+0800 Test[93703:1505747] str地址:0x604000448700,str:new--22
    2018-07-04 17:42:02.877705+0800 Test[93703:1505747] str2地址:0xa000000003232323,str2:222
    2018-07-04 17:42:02.878034+0800 Test[93703:1505747] str3地址:0x604000448700,str3:new--22
    

    上述测试说明:NSMutableString copy为深拷贝,strong仅增加引用计数,当原对象内容修改之后,strong指针同步修改

    参考链接

    https://juejin.im/entry/57b15244a633bd00570955be
    https://www.jianshu.com/p/ca7e206a2922

    相关文章

      网友评论

          本文标题:NSString copy strong

          本文链接:https://www.haomeiwen.com/subject/hoceuftx.html