objective-c shallow- and deep-copy

来源:互联网 发布:淘宝怎么买呼死你 编辑:程序博客网 时间:2024/05/20 12:50

转自:https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectCopying.html

Object Copying

Copying an object creates a new object with the same class and properties as the original object. You copy an object when you want your own version of the data that the object contains. If you receive an object from elsewhere in an application but do not copy it, you share the object with its owner (and perhaps others), who might change the encapsulated contents. If you are creating a subclass, you might consider making it possible for others to copy instances of your class. Generally, an object should be “copyable” when it is a value object—an object whose main purpose is to encapsulate some data.

Requirements for Object Copying

An object can be copied if its class adopts the NSCopying protocol and implements its single method, copyWithZone:. If a class has mutable and immutable variants, the mutable class should adopt the NSMutableCopying protocol (instead of NSCopying) and implement themutableCopyWithZone: method to ensure that copied objects remain mutable. You make a duplicate of an object by sending it a copy ormutableCopy message. These messages result in the invocation of the appropriate NSCopying or NSMutableCopying method.

Copies of objects can be shallow or deep. Both shallow- and deep-copy approaches directly duplicate scalar properties but differ on how they handle pointer references, particularly references to objects (for example, NSString *str). A deep copy duplicates the objects referenced while a shallow copy duplicates only the references to those objects. So if object A is shallow-copied to object B, object B refers to the same instance variable (or property) that object A refers to. Deep-copying objects is preferred to shallow-copying, especially with value objects.



Memory-Management Implications

Like object creation, object copying returns an object with a retain count of 1. In memory-managed code, the client copying the object is responsible for releasing the copied object. Copying an object is similar in purpose to retaining an object in that both express an ownership in the object. However, a copied object belongs exclusively to the new owner, who can mutate it freely, while a retained object is shared between the owners of the original object and clients who have retained the object.


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 皮鞋划了个口子怎么办 光面鞋划了一道怎么办 绿萝叶子太密怎么办 绿萝中间太密了怎么办 苹果mac密码忘了怎么办 mac锁屏密码忘了怎么办 剑三头发飞起来怎么办 苹果6p开不了机怎么办 苹果6p黑屏了怎么办 肚子胀反胃想吐怎么办 脸上干皮特别多怎么办 月光足还不了款怎么办 孩子不爱和小朋友玩怎么办 不知道怀孕抽烟了怎么办 校园欺凌来了该怎么办 拉的屎是绿色的怎么办 生完孩子痔疮痛怎么办 生过孩子脱肛该怎么办 生完宝宝有痔疮怎么办 毎次大便都脱肛怎么办 怀孕快生了便秘怎么办 35周孕晚期便秘怎么办 怀孕了老公出轨了怎么办 婆婆跟老公睡了怎么办 婆婆和老公互黏怎么办 15岁就掉头发怎么办 24岁经常掉头发怎么办 舍友打游戏太吵怎么办 家里的地砖想换怎么办 墙上的瓷砖掉了怎么办 瓷砖掉了一块瓷怎么办 白色裙子太透了怎么办 剑三石头插错了怎么办 四六级证书丢了怎么办 当月发票冲红了怎么办 当月发票红冲了怎么办 作废的发票扔了怎么办 发票公章盖错了怎么办 发票章盖的模糊怎么办 下的电影没字幕怎么办 荣耀8下载东西慢怎么办