您的位置:首页 > 大数据 > 人工智能

理解 retain 、copy 、NSMutableString 、NSString 的用法

2015-12-31 00:00 99 查看
copy为内容拷贝,retain为指针拷贝,NSMutableString为可变字符串,NSString为不可变字符串。

但并不表示这个对象声明的变量的值不可变,而是表示它初始化以后,你不能改变该变量所分配的内存中的值,但你可以重新分配该变量所处的内存空间。而NSMutableString是可变的,意味着你可以追加它的内存空间,或者修改它所分配的内存空间中的值。

"NSString *"这个数据类型代表一个NSString对象的指针,不是NSString对象本身。

"NSMutableString *"这个数据类型则是代表"NSMutableString"对象本身,这两者是有区别的。

这也是有的时候我们使用NSMutableString类型字符串时,要使用copy的原因,因为可能不想改变新的字符串时影响到旧的字符串的值。

不管是retain还是copy对于NSString来说都是浅拷贝,还是指向的之前的内存区域。

@property (retain, nonatomic) NSString *retainStr;
@property (copy, nonatomic)   NSString *copyStr;
@property (retain, nonatomic) NSMutableString *retainMStr;
@property (copy, nonatomic)   NSMutableString *copyMStr;

NSMutableString *mStr = [NSMutableString string];
[mStr setString:@"我没变"];

self.retainStr   = mStr;
self.copyStr     = mStr;
self.retainMStr = mStr;
self.copyMStr   = mStr;

NSLog(@"retainStr:%@",  self.retainStr);
NSLog(@"copyStr:%@",    self.copyStr);
NSLog(@"retainMStr:%@", self.retainMStr);
NSLog(@"copyMStr:%@",   self.copyMStr);
NSLog(@"\n");

[mStr setString:@"我变了"];

NSLog(@"retainStr:%@",  self.retainStr);
NSLog(@"copyStr:%@",    self.copyStr);
NSLog(@"retainMStr:%@", self.retainMStr);
NSLog(@"copyMStr:%@",   self.copyMStr);
NSLog(@"\n");

NSString *str = @"我来了";//[[NSString alloc] initWithFormat:@"我来了"];//两种方式都一样。
self.retainStr  = str;
self.copyStr    = str;
self.retainMStr = [str mutableCopy];
self.copyMStr   = [str mutableCopy];

NSLog(@"retainStr:%@",  self.retainStr);
NSLog(@"copyStr:%@",    self.copyStr);
NSLog(@"retainMStr:%@", self.retainMStr);
NSLog(@"copyMStr:%@",   self.copyMStr);
NSLog(@"\n");

str =@"我走了";//[[NSStringalloc] initWithFormat:@"我走了"];//两种方式都一样

NSLog(@"retainStr:%@",  self.retainStr);
NSLog(@"copyStr:%@",    self.copyStr);
NSLog(@"retainMStr:%@", self.retainMStr);
NSLog(@"copyMStr:%@",   self.copyMStr);
NSLog(@"\n");

//打印结果
2015-12-31 14:05:42.780 SXH[24228:1216731] retainStr:我没变
2015-12-31 14:05:43.312 SXH[24228:1216731] copyStr:我没变
2015-12-31 14:05:43.857 SXH[24228:1216731] retainMStr:我没变
2015-12-31 14:05:44.548 SXH[24228:1216731] copyMStr:我没变
2015-12-31 14:05:57.064 SXH[24228:1216731]
2015-12-31 14:06:17.624 SXH[24228:1216731] retainStr:我变了
2015-12-31 14:06:19.612 SXH[24228:1216731] copyStr:我没变
2015-12-31 14:06:24.666 SXH[24228:1216731] retainMStr:我变了
2015-12-31 14:06:27.213 SXH[24228:1216731] copyMStr:我没变
2015-12-31 14:06:29.316 SXH[24228:1216731]
2015-12-31 14:06:52.438 SXH[24228:1216731] retainStr:我来了
2015-12-31 14:06:52.962 SXH[24228:1216731] copyStr:我来了
2015-12-31 14:06:53.484 SXH[24228:1216731] retainMStr:我来了
2015-12-31 14:06:53.951 SXH[24228:1216731] copyMStr:我来了
2015-12-31 14:06:58.966 SXH[24228:1216731]
2015-12-31 14:07:53.086 SXH[24228:1216731] retainStr:我来了
2015-12-31 14:07:53.865 SXH[24228:1216731] copyStr:我来了
2015-12-31 14:07:54.383 SXH[24228:1216731] retainMStr:我来了
2015-12-31 14:07:54.964 SXH[24228:1216731] copyMStr:我来了

ps:
http://www.cnblogs.com/celestial/archive/2012/10/12/2721244.html
标签: