title: Implementing NSCopy
body: <p>I'm a little confused on the correct way to make my class support NSCopy mechanics and I'm hoping someone can help clear things up for me.</p>
  
  <p>The big question is this:
  If my class is mostly immutatble except for a collections property, when I'm implementing the 'copyWithZone' method through NSCopy inheritance, do I need to do the version where I'm returning a self that had a retain called on it like this:</p>
  
  <pre><code>- (id)copyWithZone:(NSZone *)zone 
  {
       return [self retain];
  }
  </code></pre>
  
  <p>or do I need to do the other version that takes in to consider the policies used by NSCopyObject as outlined in the Memory Management programming guide published by Apple? (I've read a number of posts about how NSCopyObject is dangerous so one has to be careful with their implementations).</p>
  
  <pre><code>- (id)copyWithZone:(NSZone *)zone
  {
      NSCell *cellCopy = NSCopyObject(self, 0, zone);
      /* Assume that other initialization takes place here. */
      cellCopy-&gt;image = nil;
      [cellCopy setImage:[self image]];
      return cellCopy;
  }
  </code></pre>
  
  <p>My class has ivars are a mix of primitive types, object types and an NSArray collection. I'm intending the class to be semi-immutable in the sense that clients cannot modify any of the intrinsic properties after the class was created initially but at some point the client will receive additional related data that needs to be appended to the class. Additionally, when clients are holding on to an instance, they should have their own copy instead of sharing it so it is a deep copy instance as opposed to a shallow copy.</p>
  
  <p>The code:</p>
  
  <pre><code>@interface MySampleClass : NSObject {
  NSString *myName;
  NSString *myTitle;
  BOOL     isAFlag;
  
  NSArray *aListOfProperites; // collection holds objects of another class named 'MySampleProperty'
  }
  
  @property (nonatomic, copy, readonly) NSString *myName;
  @property (nonatomic, copy, readonly) NSString *myTitle;
  @property (nonatomic, readonly) BOOL isAFlag;
  
  @property (nonatomic, copy, readonly) NSArray *aListOfProperties;
  
  -(id)initWithNameTitleAndFlag:(NSString *)aName title:(NSString *)aTitle flag:(BOOL)aFlag;
  
  -(void)addProperty:(MySampleProperty *)aProperty;
  @end
  
  @implementation MySampleClass
  
  -(id)initWithNameTitleAndFlag:(NSString *)aName title:(NSString *)aTitle flag:(BOOL)aFlag
  {
      self = [super init];
      if (nil != self)
      {
         [self setMyName:aName];
         [self setMyTitle:aTitle];
         [self setAFlag:aFlag];
      }
      return self;
  }
  
  // all of my object setters do a 'copy' instead of retain or assign
  -(void)setMyName:(NSString *)aNewName
  {
  if (aNewName != myName) 
  {
       [myName release];
       myName = [aNewName copy];
  }
  }
  - (id)copyWithZone:(NSZone *)zone
  {
  // do I do a simple retain on the object or follow NSCopyObject policies?
  }
  
  @end
  </code></pre>