朝花夕拾, 随着代码量的增加, 自己学习的重心逐渐从熟练度,转向对代码的理解和使用之上来.因此决定整理细嚼慢咽系列文章,以便自己学习巩固..
一般我们声明一个全局字符串变量都是这样的
@property (nonatomic, copy) NSString *copyedString;
但是为什么字符串一般都是copy呢?
我们需要先弄清楚copy的作用:
copy语法的目的:改变副本的时候,不会影响到源对象;
深拷贝:内容拷贝,会产生新的对象。新对象计数器置为1,源对象计数器不变。
浅拷贝:指针拷贝,不会产生新的对象。源对象计数器+1。
知道这些后我们看以下测试代码:
@property (nonatomic, strong) NSString *strongString;
@property (nonatomic, copy) NSString *copyedString;
@property (nonatomic, retain) NSString *retainString;
定义了三种声明不同的String;
接下来继续, 进行测试
NSString *string = [NSString stringWithFormat:@"原始值"];
self.strongString = string;
self.copyedString = string;
self.retainString = string;
NSLog(@"origin string: %p, %p", string, &string);
NSLog(@"strong string: %p, %p", _strongString, &_strongString);
NSLog(@"copy string: %p, %p", _copyedString, &_copyedString);
string = @"新值";
NSLog(@"\n originString -- %@, strongString -- %@, copyString ---%@, retainString --- %@", string, self.strongString, self.copyedString, self.retainString);
NSLog(@"\n 改变后的 origin string: %p, %p", string, &string);
NSLog(@"\n 改变后的 strong string: %p, %p", _strongString, &_strongString);
NSLog(@"\n 改变后的 copy string: %p, %p", _copyedString, &_copyedString);
打印结果:
2016-09-07 17:14:02.483 AFNetWorkingTest[2412:1184257] origin string: 0x7f80a9c87720, 0x7fff531f29e8**
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] strong string: 0x7f80a9c87720, 0x7f80a9c81410**
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] copy string: 0x7f80a9c87720, 0x7f80a9c81418**
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] originString -- 新值, strongString -- 原始值, copyString ---原始值, retainString --- 原始值
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] 改变后的**** origin string: 0x10ca53680, 0x7fff531f29e8**
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] 改变后的**** strong string: 0x7f80a9c87720, 0x7f80a9c81410**
2016-09-07 17:14:02.484 AFNetWorkingTest[2412:1184257] 改变后的**** copy string: 0x7f80a9c87720, 0x7f80a9c81418**
由此看出
转载自http://blog.csdn.net/itianyi/article/details/9018567
对源头是NSString的字符串,无论是retain声明的变量还是copy声明的变量,当第二次源头的字符串重新指向其它的地方的时候,
它还是指向原来的最初的那个位置,也就是说其实二者都是指针引用,也就是浅拷贝。
之后我们替换为NSMutableString:
NSMutableString *string = [NSMutableString stringWithFormat:@"原始值"];
self.strongString = string;
self.copyedString = string;
self.retainString = string;
NSLog(@"origin string: %p, %p", string, &string);
NSLog(@"strong string: %p, %p", _strongString, &_strongString);
NSLog(@"copy string: %p, %p", _copyedString, &_copyedString);
[string appendString:@"de"];
NSLog(@"\n originString -- %@, strongString -- %@, copyString ---%@, retainString --- %@", string, self.strongString, self.copyedString, self.retainString);
NSLog(@"\n 改变后的 origin string: %p, %p", string, &string);
NSLog(@"\n 改变后的 strong string: %p, %p", _strongString, &_strongString);
NSLog(@"\n 改变后的 copy string: %p, %p", _copyedString, &_copyedString);
看打印结果:
**2016-09-07 18:28:49.491 AFNetWorkingTest[2759:1441018] origin string: 0x7fb219627df0, 0x7fff516359e8**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] strong string: 0x7fb219627df0, 0x7fb219622540**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] copy string: 0x7fb219628230, 0x7fb219622548**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] **** originString--****原始值****de, strongString--****原始值****de, copyString---****原始值****, retainString---****原始值****de**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] **** ****改变后的**** origin string: 0x7fb219627df0, 0x7fff516359e8**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] **** ****改变后的**** strong string: 0x7fb219627df0, 0x7fb219622540**
**2016-09-07 18:28:49.492 AFNetWorkingTest[2759:1441018] **** ****改变后的**** copy string: 0x7fb219628230, 0x7fb219622548**
可以看到, copy的地址已经出现了新的内存地址.
结论:
转载自http://blog.csdn.net/itianyi/article/details/9018567
对源头是NSMutableString的字符串,retain仅仅是指针引用,增加了引用计数器,这样源头改变的时候,用这种retain方式声明的变量
(无论被赋值的变量是可变的还是不可变的),它也会跟着改变;而copy声明的变量,它不会跟着源头改变,它实际上是深拷贝。
之后, 了解到NSString 编译后是一个
__NSCFConstantString
对象,
而NSMutableString 是一个
__NSCFString
对象
具体请看
https://blog.cnbluebox.com/blog/2014/04/16/nsstringte-xing-fen-xi-xue-xi/
http://www.cocoachina.com/ios/20150512/11805.html
感谢
参考,转载博客:
http://blog.csdn.net/itianyi/article/details/9018567
http://stackoverflow.com/questions/2521468/nsstring-copy-not-copying
https://blog.cnbluebox.com/blog/2014/04/16/nsstringte-xing-fen-xi-xue-xi/
网友评论