Copying

pp135 这章比较水 没看懂例子。。

The Copying pattern is used to create new object instances by copying existing instances.

 

Motivation

Use the  Copying pattern any time you want to create a new object that is a copy of another object. Copying captures the state of an object at a moment in time.

 

C and therefore Objective-C always passes arguments by values. Even C structures like Cocoa's NSRect are passed by value and implicitly copied. However, it's possible to paass a pointer to a value and then modify a value indirectly via the pointer. The pointer itself is implicitly copied, but the value the pointer addresses is not copied. 

Objective-C objects are always passed as pointers. 

If you are writing a function or method that will store a pointer to an object, you may actually want to create a copy and store a pointer to the copy. That way, changes that are later made to the original object won't affect the stored copy.

 

Solution

A shallow copy is a copy of the object itself, but not any objects contained by the object being copied. In other words, when an object is shallow copied, the result is a new object that contains pointers to the exact same objects contained by the first.

A deep copy copies the cintained objects as well. The result of a deep copy is a new object that contains pointers to copies of the objects contained by the original. Usually, deep copies are as deep as possible. Objects within objects within objects are also copied to as deep a level as necessary to copy every object in the containment hierarchy.

Cocoa classes that support copying all implement the Copying pattern to return a shallow copy. The result of copying an NSArray instance is a new instance that contains pointers to the same objects as the original. 

 

One convenient way to obtain deep copies of Cocoa objects uses the Archiving and Unarchiving pattern explained in Chapter 11. If object to be copied and all the objectscontained within that object conform to Cocoa's NSCoding protocol, the following code will produce a deep copy. NSCoding is the Objective-C protocol needed to support Archiving and Unarchiving.

id MyDeepCopyObject( id <NSCoding> anObject)

//This function accepts any object conforming the NSCoding protocol 

// and returns a deep copy of that object. The object returned must 

// be explicitly released unless automatic garbage collection is used.

{

  return [[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver

      archivedDataWithRootObject:anObject]] retain];

}

 If an obejct is immutable and therfore can't be changed, there is no need to make a copy. It's safe to sotre the pointer to the immutable object without making a copy. Immutable objects can be used as if they were ordinary pass-by-value C language data types.

If a Cocoa object has both mutable and immutable variants, and the object is copied, the resulting new object is always immutable. For example, if you sned the -copy message to an NSMutableSet instance, the object returned is an instance of the immutable NSSet class.

In some cases, a -(id)mutableCopy method is avaiable that will returna mutable copy. For example, sending the -mutableCopy message to either an NSMutableSet or an NSSet will return an instance of NSMutableSet.

 

 

 

 

 

 

 

 

 

 

posted on 2013-02-24 23:03  Chansonyan  阅读(279)  评论(0)    收藏  举报

导航