`

object c 中 retain 和copy的区别

    博客分类:
  • IOS
 
阅读更多


原来简单解释过属性定义(Property) ,并且提起了简单的retain,copy,assign的区别。那究竟是有什么区别呢?
assign就不用说了,因为基本上是为简单数据类型准备的,而不是NS对象们。
Retain vs. Copy!!

copy: 建立一个索引计数为1的对象,然后释放旧对象
retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1

那上面的是什么该死的意思呢?
Copy其实是建立了一个相同的对象,而retain不是:
比如一个NSString对象,地址为0×1111,内容为@”STR”
Copy到另外一个NSString之后,地址为0×2222,内容相同,新的对象retain为1,旧有对象没有变化
retain到另外一个NSString之后,地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的retain值+1
也就是说,retain是指针拷贝,copy是内容拷贝。哇,比想象的简单多了…
分享到:
评论

相关推荐

    iPhone开发、ObjectiveC_面试题目

    7、定义属性时,什么情况使用copy,assign,和retain 8、autorelease的对象是在什么时候被release的? 9、这段代码有什么问题,如何修改 10、for (int i = 0; i ; i++) { NSString *string = @”Abc”; ...

    Google Objective-C Style Guide 中文版

    Google Objective-C Style Guide 中文版 目录 例子 空格与格式 空格与制表符 行宽 方法声明与定义 ...Setters中对NSString进行copy 避免抛出异常 nil的检查 BOOL陷阱 属性 Cocoa模式 委托模式 模型-视图-控制器

    简述iOS属性中的内存管理参数

    此属性只能用于Object-C对象类型。 三,copy 在赋值时,将新值复制一份,复制工作由copy执行,此属性只对那些实行了NSCopying协议的对象类型有效。 总结 以上所述是小编给大家介绍的iOS属性中的内存管理参数 ,希望...

    leetcode题库-iOS-Interview-Questions:收集整理iOS笔试面试题

    retain区别 HTTP的数据传输 动态绑定 subclass category protocol extension 区别 单例的实现 performSelector:withObject:方法的作用是什么? 解释一下多态 如何实现深拷贝 http和socket通信的区别,tcp和udp的区别...

    flash shiti

    11. 全等(===)运算符和相同运算符基本相似,但是它们有一个很重要的区别 □ A. 全等(===)运算符执行数据类型的转换 □ B. 全等(===)运算符不执行数据类型的转换 □ C. 全等(===)运算符永远返回...

    silverLIght控件包

    (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software. (D) If you distribute any portion of the ...

    无人永生源代码 v1.003

    cshell.dll, and object.lto should be placed in the root of your NOLF resource directory to be used by the game (i.e., in the root of the NOLF.rez file). The ClientShellDLL and Object projects ...

    BlueToolInstall

    1.6. “Object Code” means those portions of the Software, if any, furnished to Licensee in object code or machine readable form, including, without limitation, any bit images or binary files for ...

    struts1.3.9.zip

    (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those ...

    Struts2上传所需jar包

    (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those ...

    王小平版遗传算法的光盘源代码

    parameters will retain the default values. Remember you must specify either a population params file or `none on the command line for each population. Note: since the random seed value is not a ...

    Java邮件开发Fundamentals of the JavaMail API

    consulting for JZ Ventures, Inc. His latest book is titled Java Collections from Apress . Fundamentals of the JavaMail API Page 2 Presented by developerWorks, your source for great tutorials ibm...

    apktool documentation

    Used with -c / --copy-original on [b]uild unknown = Files / folders that are not part of the standard AOSP build procedure. These files will be injected back into the rebuilt APK. apktool.yml ...

    VB编程资源大全(英文源码 控制)

    <END><br>46 , dcsize.zip When this control is put onto a form, it ensures that all other controls retain the correct aspect-ratio from the design time view. Basically, it resize all controls on a ...

    VB编程资源大全(英文源码 其它)

    Copy the file with .scr extension to your computers win directory (Windows for 98, Winnt for NT) and check in screens saver settings.<END><br>73,Cls_sample_Collection.zip Implement with Class and ...

Global site tag (gtag.js) - Google Analytics