copy 和 strong(或retain)的区别
http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertynonatomic-strong-fo
'copy' will cause the setter for that property to create a copy of the object, and is otherwise identical to strong. You would use this to make sure that if someone sets your property to a mutable string, then mutates the string, you still have the original value. If the string isn't mutable, Cocoa will silently optimize out the copy operation, which is nice :)
'strong' will keep the property's value alive until it's set to something else. If you want incoming mutable strings to change out from under you (not impossible, but not all that common, a thing to want), then strong would be the right thing to do. Generally strong is more useful for objects that represent something more complex than a simple "value" (i.e. not NSString, NSNumber, NSValue, etc...).
'assign' is the default (and indeed only) possible setting for an integer. Integers can't be retained or copied like objects.
For attributes whose type is an immutable value class that conforms to the NSCopying protocol, you almost always should specify copy in your @property declaration. Specifying retain is something you almost never want in such a situation.In non ARC strong will work like retain
Here's why you want to do that:
NSMutableString *someName = [NSMutableString stringWithString:@"Chris"];
Person *p = [[[Person alloc] init] autorelease];
p.name = someName;
[someName setString:@"Debajit"];
The current value of the Person.name property will be different depending on whether the property is declared retain or copy — it will be @"Debajit" if the property is marked retain, but @"Chris" if the property is marked copy.
Since in almost all cases you want to prevent mutating an object's attributes behind its back, you should mark the properties representing them copy. (And if you write the setter yourself instead of using @synthesize you should remember to actually use copy instead of retain in it.)
copy 和 strong(或retain)的区别的更多相关文章
- iOS中copy和strong修饰符的区别
iOS中copy和strong修饰符的区别 //用copys修饰的生成的都是不可变的对象 ,如果调用可变类型方法的直接报错 @property(nonatomic,copy)NSString * cp ...
- ios OC 关键字 copy,strong,weak,assign的区别
一.先介绍 copy.strong.weak 的区别,如代码所示 @property(copy,nonatomic)NSMutableString*aCopyMStr; @property(stron ...
- property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...
- iOS property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...
- iOS内存管理retain,assign,copy,strong,weak
转自:http://www.cnblogs.com/nonato/archive/2013/11/28/3447162.html iOS的对象都继承于NSObject, 该对象有一个方法:retain ...
- [OC] @property时,copy、strong、weak、assign的区别
@property(copy,nonatomic)NSMutableString*copyStr; @property(strong,nonatomic)NSMutableString*strongS ...
- assign, retain, copy, weak, strong
一.assign, retain, copy 的区别(引用计数 RC reference count) 参考:IOS基础:retain,copy,assign及autorelease 1. 假设你用m ...
- 探究@property申明对象属性时copy与strong的区别
一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...
- @proprety数组字典字符串用copy和strong区别(深浅拷贝)
//// @proprety数组字典字符串用copy和strong区别(深浅拷贝).h// IOS笔记//// /* _proprety________copy_strong_________h ...
- OC:copy 与 retain 的区别
copy与retain的区别: copy是创建一个新对象,retain是创建一个指针,引用对象计数加1.Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的引用计数无关,旧有对象 ...
随机推荐
- Android:如何从堆栈中还原ProGuard混淆后的代码
本文翻译自Android: How To Decode ProGuard's Obfuscated Code From Stack Trace 本篇文章是写给那些在他们的应用中使用ProGuard并且 ...
- linux下批量查找/替换文本内容
一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令: 1.批量查找某个目下文 ...
- centos 7 卸载 mariadb 的正确命令
#列出所有被安装的rpm package rpm -qa | grep mariadb #卸载 rpm -e mariadb-libs-5.5.37-1.el7_0.x86_64 错误:依赖检测失败: ...
- linux中mysql密码找回的两种方式
方法一:修改my.cnf配置文件 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 ...
- main函数读取jar包外部的配置文件properties
首先,Java的main方法有个初始化入参args,如下所示: public static void main(String[] args) {} 然后,在linux下执行jar包引入外部配置文件的命 ...
- 有了Hadoop MapReduce, 为什么还要Spark?
a. 由于MapReduce的shuffle过程需写磁盘,比较影响性能:而Spark利用RDD技术,计算在内存中进行. b. MapReduce计算框架(API)比较局限, 而Spark则是具备灵活性 ...
- oracle 12541,12560,00511无监听程序, 协议适配器错误问题分析及解决方案
oracle 12541,12560,00511无监听程序, 协议适配器错误问题分析及解决方案 问题描述: 1. lsnrctl start.stop.非常慢,出现卡顿现象: 2. 执行lsnrc ...
- joomla3.1安装不通过Magic Quotes GPC解决方法
测试安装下joomla 3.1稳定版,但是不能成功,Magic Quotes GPC始终显示 否红色,这样就安装不了了! 要解决这个很简单,开启Magic Quotes GPC就行了,于是找到php. ...
- JS基础回顾,小练习(去除字符串空格)
方法1: var str = ' h t m l 5 '; function trim(str) { var reg = /(\s+)/g; var m,s = str; while(m = reg. ...
- Lua面向对象设计
首先对于Lua语言,它没有打算被用来进行大型的程序设计,相反,Lua目标定于小型到中型的程序设计,通常是作为大型系统的一部分,所以它只提供了一套精简的元素,很多高级语言的概念都没有.这样Lua就成为了 ...