iOS DevelopmentiOS点点滴滴iOS Developer

iOS小知识(三):NSString用copy还是strong修

2018-02-27  本文已影响210人  里克尔梅西

之前在项目的model中,也都没有在意这个问题,直接所有的NSString属性都是用的strong来修饰的,其实这是不对的,后来经同事提点,说是最好model里面NSSring属性都要用copy来修饰,下面我们就来探究一下其中的原因。

结论

首先就直接上结论:

代码示例

声明两个属性变量

@property (nonatomic, strong) NSString *strongStr;
@property (nonatomic, copy)   NSString *copyedStr;
//不可变字符串赋值
- (void)testString {
    NSString *string = [NSString stringWithFormat:@"lalala"];
    self.strongStr = string;
    self.copyedStr = string;
        
    NSLog(@"origin string: %p, %p", string, &string);
    NSLog(@"strong string: %p, %p", _strongStr, &_strongStr);
    NSLog(@"copyed string: %p, %p", _copyedStr, &_copyedStr);
}

可以看到结果如下:


image.png

这种情况下,不管是strong还是copy属性的对象,其指向的地址都是同一个,即为string指向的地址。

而如果将string换成NSMutableString:

//可变字符串赋值
- (void)testMutbleString {
    NSMutableString *mutbleString = [NSMutableString stringWithFormat:@"hahaha"];
    self.strongStr = mutbleString;
    self.copyedStr = mutbleString;
    
//    [mutbleString appendString:@" wawawa"];
    
    NSLog(@"mut origin string: %p, %p", mutbleString, &mutbleString);
    NSLog(@"mut strong string: %p, %p", _strongStr, &_strongStr);
    NSLog(@"mut copyed string: %p, %p", _copyedStr, &_copyedStr);
}
image.png

可以发现,此时copy属性字符串已不再指向string字符串对象,而是深拷贝了string字符串,并让_copyedStr对象指向这个字符串。

而如果此时我们改变mutbleString的值,即放开上面代码中的注释[mutbleString appendString:@" wawawa"];

image.png

可以看到如下图,_strongStr因为与string指向同一对象,所以其值跟随string发生了变化;而_copyedStr因为创建了一个新的对象,所以不会跟随string的值变化而变化。

参考文章:
https://southpeak.github.io/2015/05/10/ios-techset-1/
https://www.jianshu.com/p/a4874c788f78

上一篇 下一篇

猜你喜欢

热点阅读