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.
-
1The great [TUTORIAL](http://www.techotopia.com/index.php/Copying_Objects_in_Objective-C) for understanding copy, mutableCopy and copyWithZone – horkavlna Sep 14 '13 at 08:35
6 Answers
As always with reference types, there are two notions of "copy". I'm sure you know them, but for completeness.
- 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.
- 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) a 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 latter. 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;
}

- 15,174
- 22
- 76
- 112

- 48,938
- 12
- 131
- 152
-
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 Dec 07 '09 at 22:37
-
30@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 Aug 10 '10 at 16:44
-
2@Adam Is there a reason to use `alloc` instead of `allocWithZone:` since the zone was passed in? – Richard Aug 12 '11 at 19:04
-
3Well, zones are effectively unused in modern OS X based runtimes (i.e. I think they're literally never used). But yes, you could call `allocWithZone`. – Adam Wright Aug 12 '11 at 22:04
-
supposing my 'YourClass' contains 10 more properties, I have to set all of them in my 'copyWithZone' implementation? – acecapades Nov 19 '12 at 01:31
-
This works perfect for me, except for if I try to copy an object that has a UIImageView or UIView as a property. Those objects don't have `copyWithZone:` so I can't do `another.img = [img copyWithZone: zone];` and when I use `another.img = [img copy];` it doesn't compile, check out this question: http://stackoverflow.com/questions/9017944/deep-copy-objective-c-objects-with-uiimageview-as-property – SirRupertIII Jan 23 '13 at 22:30
-
1[For completeness, the tutorial for understanding `copy`, `mutableCopy` and `copyWithZone`, linked to in one of the answers, is here.](http://www.techotopia.com/index.php/Copying_Objects_in_Objective-C) – Whymarrh Mar 13 '14 at 01:12
-
Objective-C has no way to make a bitwise (memory) 'shallow'-copy. You are stuck with explicitly assigning each variable in your class. This example also shows why the copy(WithZone) is bogus: How to implement copyWithZone for a subclass of "YourClass". To initialize its member variables it should call its [super copyWithZone], but that calls alloc on its class, which is wrong, because it allocates the wrong object. There is no correct way to implement this. Instead use a solution in the form of [initAsShallow copy as I described here](http://stackoverflow.com/a/39111473/2396744). – isgoed Aug 24 '16 at 08:39
-
@SebastienPeek Yes, but with the default zone. Should not matter as zones are not used anyway, but it is not according to specification. – isgoed Aug 24 '16 at 08:44
-
For those who, like me, arrived here and asked "wait, what is a zone", [here](https://developer.apple.com/reference/objectivec/1418956-nsobject/1571954-zone?language=objc) is the Apple Developer doc page for "zone". Zones are deprecated and hardly used at all anymore in Objective C (maybe not at all by the time you read this), but they remain as parameters for compatibility reasons. – Brendan Goggin Apr 12 '17 at 19:41
-
To create the actual copy, use `[instance copy]`. That wasn't obvious from the answer for me... – bbjay Apr 29 '17 at 10:22
-
got a error : No visible @interface for 'NSObject' declares the selector 'copyWithZone:' – sodino Sep 08 '22 at 03:18
Apple documentation says
A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject.
to add to the existing answer
@interface YourClass : NSObject <NSCopying>
{
SomeOtherObject *obj;
}
// In the implementation
-(id)copyWithZone:(NSZone *)zone
{
YourClass *another = [super copyWithZone:zone];
another.obj = [obj copyWithZone: zone];
return another;
}

- 2,799
- 2
- 27
- 41
-
2Since YourClass descends directly from NSObject I don't think it's necessary here – Mike Jan 25 '12 at 10:05
-
2Good point, but its a general rule, in case its a long class hierarchy. – Saqib Saud Jan 27 '12 at 06:59
-
8I got an error: `No visible @interface for 'NSObject' declares the selector 'copyWithZone:'`. I guess this is only required when we are inheriting from some other custom class that implements `copyWithZone` – Sam Oct 05 '13 at 06:09
-
1another.obj = [[obj copyWithZone: zone]autorelease]; for all subclasses of NSObject. And for primitive datatypes you just assign them -> another.someBOOL = self.someBOOL; – hariszaman Jul 29 '14 at 09:14
-
@Sam "NSObject does not itself support the NSCopying protocol. Subclasses must support the protocol and implement the copyWithZone: method. A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject." https://developer.apple.com/documentation/objectivec/nsobject/1418807-copy?language=objc – s4mt6 Apr 25 '20 at 05:56
I don't know the difference between that code and mine, but I have problems with that solution, so I read a little bit more and found that we have to set the object before return it. I mean something like:
#import <Foundation/Foundation.h>
@interface YourObject : NSObject <NSCopying>
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *line;
@property (strong, nonatomic) NSMutableString *tags;
@property (strong, nonatomic) NSString *htmlSource;
@property (strong, nonatomic) NSMutableString *obj;
-(id) copyWithZone: (NSZone *) zone;
@end
@implementation YourObject
-(id) copyWithZone: (NSZone *) zone
{
YourObject *copy = [[YourObject allocWithZone: zone] init];
[copy setNombre: self.name];
[copy setLinea: self.line];
[copy setTags: self.tags];
[copy setHtmlSource: self.htmlSource];
return copy;
}
I added this answer because I have a lot of problems with this issue and I have no clue about why is it happening. I don't know the difference, but it's working for me and maybe it can be useful for others too : )

- 430
- 5
- 15
another.obj = [obj copyWithZone: zone];
I think, that this line causes memory leak, because you access to obj
through property which is (I assume) declared as retain
. So, retain count will be increased by property and copyWithZone
.
I believe it should be:
another.obj = [[obj copyWithZone: zone] autorelease];
or:
SomeOtherObject *temp = [obj copyWithZone: zone];
another.obj = temp;
[temp release];
-
Nope, methods alloc, copy, mutableCopy, new should return non-autoreleased objects. – kovpas Aug 20 '12 at 20:29
-
@kovpas, are You sure, that You understand me right? I'm not talking about returned object, I'm talking about it's data fields. – Szuwar_Jr Aug 21 '12 at 08:29
-
yeah, my bad, sorry. could you please edit your answer somehow so I could remove minus? :)) – kovpas Aug 21 '12 at 09:22
There is also the use of the -> operator for copying. For Example:
-(id)copyWithZone:(NSZone*)zone
{
MYClass* copy = [MYClass new];
copy->_property1 = self->_property1;
...
copy->_propertyN = self->_propertyN;
return copy;
}
The reasoning here is the resulting copied object should reflect the state of the original object. The "." operator could introduce side effects as this one calls getters which in turn may contain logic.

- 18,750
- 9
- 86
- 81
This is probably unpopular way. But here how I do it:
object1 = // object to copy
YourClass *object2 = [[YourClass alloc] init];
object2.property1 = object1.property1;
object2.property2 = object1.property2;
..
etc.
Quite simple and straight forward. :P

- 7,545
- 8
- 50
- 85