首先创建两个属性
@interface ViewController ()
@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);
NSLog(@"改变了");
//改变string的值
string = @"123";
NSLog(@"originString11: %p, %p,%@", string, &string,string);
NSLog(@"strongString11: %p, %p,%@", _strongStr,&_strongStr,self.strongStr);
NSLog(@"copyString11: %p, %p,%@", _copyedStr,&_copyedStr,self.copyedStr);
}
打印:
2019-02-26 15:02:52.967697+0800 练习[55619:3334557] originString1: 0x90a0d7756faa2ec7, 0x7ffee990ea18,abc
2019-02-26 15:02:52.967820+0800 练习[55619:3334557] strongString1: 0x90a0d7756faa2ec7, 0x7ff074c0a7a0,abc
2019-02-26 15:02:52.967911+0800 练习[55619:3334557] copyString1: 0x90a0d7756faa2ec7, 0x7ff074c0a7a8,abc
2019-02-26 15:02:52.968003+0800 练习[55619:3334557] 改变了
2019-02-26 15:02:52.968072+0800 练习[55619:3334557] originString11: 0x1062f0110, 0x7ffee990ea18,123
2019-02-26 15:02:52.968133+0800 练习[55619:3334557] strongString11: 0x90a0d7756faa2ec7, 0x7ff074c0a7a0,abc
2019-02-26 15:02:52.968214+0800 练习[55619:3334557] copyString11: 0x90a0d7756faa2ec7, 0x7ff074c0a7a8,abc
结论:
当string为不可变字符串时
- 不管是strong还是copy属性的对象,其指向的地址都是同一个,即为string指向的地址。
- 当string的值发生改变时,两个对象的值也保持原来的值
- 如果我们换作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);
NSLog(@"改变了");
//改变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);
}
打印:
2019-02-26 15:05:23.905901+0800 练习[55640:3336240] originString2: 0x60000247d7d0,0x7ffee70dfa18,abc
2019-02-26 15:05:23.906019+0800 练习[55640:3336240] strongString2: 0x60000247d7d0,0x7fb35d6090c0,abc
2019-02-26 15:05:23.906115+0800 练习[55640:3336240] copyString2: 0x84b59d1877d98e2d,0x7fb35d6090c8,abc
2019-02-26 15:05:23.906190+0800 练习[55640:3336240] 改变了
2019-02-26 15:05:23.906264+0800 练习[55640:3336240] originString2: 0x60000247d7d0,0x7ffee70dfa18,abc123
2019-02-26 15:05:23.906329+0800 练习[55640:3336240] strongString2: 0x60000247d7d0,0x7fb35d6090c0,abc123
2019-02-26 15:05:23.906405+0800 练习[55640:3336240] copyString2: 0x84b59d1877d98e2d,0x7fb35d6090c8,abc
结论:
当string为可变字符串时
- 此时copy属性字符串已不再指向string字符串对象,而是深拷贝了string字符串,并让_copyedStr对象指向这个字符串
- _strongStr与string是指向同一对象,所以_strongString的值也会跟随着改变(需要注意的是,此时_strongStr的类型实际上是NSMutableString,而不是NSString);而_copyedStr是指向另一个对象的,所以并不会改变。
- 在MRC环境下,打印两者的引用计数,可以看到string对象的引用计数是2,而_copyedStr对象的引用计数是1。
总结
在声明NSString属性时,到底是选择strong还是copy,可以根据实际情况来定。不过,一般我们将对象声明为NSString时,都不希望它改变,所以大多数情况下,我们建议用copy,以免因可变字符串的修改导致的一些非预期问题。
网友评论