To support copying, you adopt the NSCopying
protocol. The protocol has a single method—copyWithZone:
—that you must implement to allow your objects to be copied. If your application distinguishes between mutable and immutable versions of an entity, you should adopt NSCopying
for the immutable version, and NSMutableCopying
for the mutable version (you implement mutableCopyWithZone:
). The implementation of the copy
method is described in detail in Implementing Object Copy.
A "cheap" way to provide customized copy behavior in some situations is to use key-value coding. You can create a dictionary representation of an object using dictionaryWithValuesForKeys:
, create a new uninitialized instance of the same class, then use the dictionary to initialize the new instance with setValuesForKeysWithDictionary:
.
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)