NSString属性声明中的copy和retain区别
来源:互联网 发布:费用统计软件免费 编辑:程序博客网 时间:2024/05/21 09:04
转自:http://www.cnblogs.com/celestial/archive/2012/10/12/2721244.html
前几天去面试,被问到了NSString属性声明中的copy和retain具体区别,对内存计数的影响,汗,当时没整明白,也没答出来,只知道NSString一般用copy.
首先做以下几个变量声明:
@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");
打印结果如下:
2012-10-12 13:53:14.858 StringDemo[1515:11303] retainStr:我没变
2012-10-12 13:53:14.860 StringDemo[1515:11303] copyStr:我没变
2012-10-12 13:53:14.861 StringDemo[1515:11303] retainMStr:我没变
2012-10-12 13:53:14.862 StringDemo[1515:11303] copyMStr:我没变
2012-10-12 13:53:14.863 StringDemo[1515:11303]
2012-10-12 13:53:14.864 StringDemo[1515:11303] retainStr:我变了
2012-10-12 13:53:14.865 StringDemo[1515:11303] copyStr:我没变
2012-10-12 13:53:14.866 StringDemo[1515:11303] retainMStr:我变了
2012-10-12 13:53:14.867 StringDemo[1515:11303] copyMStr:我没变
2012-10-12 13:53:14.868 StringDemo[1515:11303]
2012-10-12 13:53:14.869 StringDemo[1515:11303] retainStr:我来了
2012-10-12 13:53:14.869 StringDemo[1515:11303] copyStr:我来了
2012-10-12 13:53:14.870 StringDemo[1515:11303] retainMStr:我来了
2012-10-12 13:53:14.871 StringDemo[1515:11303] copyMStr:我来了
2012-10-12 13:53:14.872 StringDemo[1515:11303]
2012-10-12 13:53:14.873 StringDemo[1515:11303] retainStr:我来了
2012-10-12 13:53:14.874 StringDemo[1515:11303] copyStr:我来了
2012-10-12 13:53:14.875 StringDemo[1515:11303] retainMStr:我来了
2012-10-12 13:53:14.876 StringDemo[1515:11303] copyMStr:我来了
由此可以看出:对源头是NSMutableString的字符串,retain仅仅是指针引用,增加了引用计数器,这样源头改变的时候,用这种retain方式声明的变量(无论被赋值的变量是可变的还是不可变的),它也会跟着改变;而copy声明的变量,它不会跟着源头改变,它实际上是深拷贝。对源头是NSString的字符串,无论是retain声明的变量还是copy声明的变量,当第二次源头的字符串重新指向其它的地方的时候,它还是指向原来的最初的那个位置,也就是说其实二者都是指针引用,也就是浅拷贝。
另外说明一下,这两者对内存计数的影响都是一样的,都会增加内存引用计数,都需要在最后的时候做处理。
其实说白了,对字符串为啥要用这两种方式?我觉得还是一个安全问题,比如声明的一个NSString *str变量,然后把一个NSMutableString *mStr变量的赋值给它了,如果要求str跟着mStr变化,那么就用retain;如果str不能跟着mStr一起变化,那就用copy。而对于要把NSString类型的字符串赋值给str,那两都没啥区别。不会影响安全性,内存管理也一样。
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- NSString属性声明中的copy和retain区别
- OC语法--问NSString属性声明中的copy和retain具体区别
- NSString copy和retain属性
- 属性特性copy和retain的区别
- NSString 的copy和retain
- 使用COPY声明NSSTRING属性
- 使用COPY声明NSSTRING属性
- 声明NSString属性用 Copy 与 strong 的区别
- 使用copy与strong声明NSString属性的区别
- NSString属性变量的特性copy retain
- 浅谈数据库调优
- 利用rowid去重
- 三大开源Linux防火墙生成器
- 题目1445:How Many Tables
- CentOS系统中postfix日志位置设置
- NSString属性声明中的copy和retain区别
- java-httpclient通过title实现从baidu爬取相关网页链接
- Objective—C NSNumber 总结
- 根据时间提取介于某个时间段的信息
- mp4 avc格式_sps_pps
- annotation(@Retention)
- 二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree)的比较
- 完善细节,使你的网站清新脱俗!
- 各编译器对C++11的支持比较