views:

854

answers:

1

I need to deep copy a custom object that has objects of its own. I've been reading around and am a bit confused as to how to inherit NSCopying and how to use NSCopyObject. Could someone help me out? Thanks for reading!

+5  A: 

As always with reference types, there are two notions of "copy". I'm sure you know them, but for completeness.

  1. A bitwise copy. In this, we just copy the memory bit for bit - this is what NSCopyObject does. Nearly always, it's not what you want. Objects have internal state, other objects, etc, and often make assumptions that they're the only ones holding references to that data. Bitwise copies break this assumption.
  2. A deep, logical copy. In this, we make a copy of the object, but without actually doing it bit by bit - we want an object that behaves the same for all intents and purposes, but isn't (necessarily) an memory-identical clone of the original - the Objective C manual calls such an object "functionally independent" from it's original. Because the mechanisms for making these "intelligent" copies varies from class to class, we ask the objects themselves to perform them. This is the NSCopying protocol.

You want the later. If this is one of your own objects, you need simply adopt the protocol NSCopying and implement + (id)copyWithZone:(NSZone *)zone. You're free to do whatever you want - though the idea is you make a real copy of yourself and return it. You call copyWithZone on all your fields, to make a deep copy. A simple example is

@interface YourClass : NSObject < NSCopying > 
{
   SomeOtherObject *obj;
}

// In the implementation
(id)copyWithZone:(NSZone *)zone
{
  // We'll ignore the zone for now
  YourClass *another = [[YourClass alloc] init];
  another->obj = [obj copyWithZone: zone];

  return another;
}
Adam Wright
But you're making the recipient of the copied object responsible to free it!Shouldn't you `autorelease` it, or am I missing something here?
bobobobo
@bobobobo: No, the fundamental rule of Objective-C memory management is: You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy”. `copyWithZone:` meets this criteria, therefore it must return an object with a retain count of +1.
Steve Madsen