美文网首页iOS 面试
iOS-NSString使用Copy和Strong修饰的区别

iOS-NSString使用Copy和Strong修饰的区别

作者: Simple_Code | 来源:发表于2017-06-28 12:07 被阅读1506次

    先看两个例子:

    首先创建两个属性

    @interface ExampleViewController ()
    @property (nonatomic, strong) NSString *strongStr;
    @property (nonatomic, copy) NSString *copyedStr;
    @end
    

    例一:

    - (void)testOne {
    NSString *string = [NSString stringWithFormat:@"abc"];
    self.strongStr = string;
    self.copyedStr = string;
    NSLog(@"originString1: %p, %p,%@", string,&string,string);
    NSLog(@"strongString1: %p, %p,%@", _strongStr,&_strongStr,self.strongStr);
    NSLog(@"copyString1:   %p, %p,%@", _copyedStr,&_copyedStr,self.copyedStr);
    
    //改变string的值
    string = @"123";
    
    NSLog(@"originString11: %p, %p,%@", string, &string,string);
    NSLog(@"strongString11: %p, %p,%@", _strongStr,&_strongStr,self.strongStr);
    NSLog(@"copyString11:   %p, %p,%@", _copyedString,&_copyedStr,self.copyedStr);
    }
    

    打印结果:

    //string创建
    originString1:  0xa000000006362613, 0x7fff54c687e8,abc
    strongString1:  0xa000000006362613, 0x7fccb66028e8,abc
    copyString1:    0xa000000006362613, 0x7fccb66028f0,abc
    
    //string改变
    originString11: 0x10b12f300,        0x7fff54c687e8,123
    strongString11: 0xa000000006362613, 0x7fccb66028e8,abc
    copyString11:   0xa000000006362613, 0x7fccb66028f0,abc
    
    结论:

    当string为不可变字符串时

    1. 不管是strong还是copy属性的对象,其指向的地址都是同一个,即为string指向的地址。
    2. 当string的值发生改变时,两个对象的值也保持原来的值
    3. 如果我们换作MRC环境,打印string的引用计数的话,会看到其引用计数值是3,即strong操作和copy操作都使原字符串对象的引用计数值加了1

    例二

    - (void)testTwo {
    NSMutableString *string= [[NSMutableString alloc]initWithString:@"abc"];
    self.strongStr = string;
    self.copyedStr = string;
    NSLog(@"originString2: %p,%p,%@", string, &string,string);
    NSLog(@"strongString2: %p,%p,%@", _strongStr,&_strongStr,self.strongStr);
    NSLog(@"copyString2:   %p,%p,%@", _copyedStr,&_copyedStr,self.copyedStr);
    
    //改变string的值
    [string appendFormat:@"%@",@"123"];
    
    NSLog(@"originString2: %p,%p,%@", string, &string,string);
    NSLog(@"strongString2: %p,%p,%@", _strongStr,&_strongStr,self.strongStr);
    NSLog(@"copyString2:   %p,%p,%@", _copyedStr,&_copyedStr,self.copyedStr);
    }
    

    打印结果:

    //string创建
    originString2: 0x60000007b940,     0x7fff5cd7b7e8, abc
    strongString2: 0x60000007b940,     0x7fed6e508668, abc
    copyString2:   0xa000000006362613, 0x7fed6e508670, abc
    
    //string改变
    originString2: 0x60000007b940,     0x7fff5cd7b7e8, abc123
    strongString2: 0x60000007b940,     0x7fed6e508668, abc123
    copyString2:   0xa000000006362613, 0x7fed6e508670, abc
    
    结论:

    当string为可变字符串时

    1. 此时copy属性字符串已不再指向string字符串对象,而是深拷贝了string字符串,并让_copyedStr对象指向这个字符串
    2. _strongStr与string是指向同一对象,所以_strongString的值也会跟随着改变(需要注意的是,此时_strongStr的类型实际上是NSMutableString,而不是NSString);而_copyedStr是指向另一个对象的,所以并不会改变。
    3. 在MRC环境下,打印两者的引用计数,可以看到string对象的引用计数是2,而_copyedStr对象的引用计数是1。

    总结

    在声明NSString属性时,到底是选择strong还是copy,可以根据实际情况来定。不过,一般我们将对象声明为NSString时,都不希望它改变,所以大多数情况下,我们建议用copy,以免因可变字符串的修改导致的一些非预期问题。

    参考文章
    http://www.cocoachina.com/ios/20150512/11805.html

    相关文章

      网友评论

        本文标题:iOS-NSString使用Copy和Strong修饰的区别

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