PDA

View Full Version : Problem copying an object (Cocoa) [Solved - needed to implement NSCopying]




Eraserhead
Sep 28, 2006, 02:28 PM
I am making a program in XCode (Obj C) and I have created an object in a program which I am going to call MyObject, however when I run this code:

MyObject *newObj;
newObj=[[MyObject alloc] init];
//newObj then gets passed to a different method.
newObj=[newObj copy];

at this point, when the program is running it errors giving this error twice:

2006-09-28 20:13:51.103 MyProgram[1333] *** -[MyObject copyWithZone:]: selector not recognized [self = 0x5f03d80]
2006-09-28 20:13:51.116 MyProgram[1333] *** -[MyObject copyWithZone:]: selector not recognized [self = 0x5f03d80]

(where 0x5f03d80 is the object I am trying to copy.)

What is going wrong and what do I need to do to make it work?



HexMonkey
Sep 28, 2006, 03:38 PM
Your class needs to implement the NSCopying protocol.

From the NSCopying documentation:

The NSCopying protocol declares a method for providing functional copies of an object. The exact meaning of “copy” can vary from class to class, but a copy must be a functionally independent object with values identical to the original at the time the copy was made. A copy produced with NSCopying is implicitly retained by the sender, who is responsible for releasing it.

NSCopying declares one method, copyWithZone:, but copying is commonly invoked with the convenience method copy. The copy method is defined for all objects inheriting from NSObject and simply invokes copyWithZone: with the default zone.

Your options for implementing this protocol are as follows:

Implement NSCopying using alloc and init... in classes that don’t inherit copyWithZone:.

Implement NSCopying by invoking the superclass’s copyWithZone: when NSCopying behavior is inherited. If the superclass implementation might use the NSCopyObject function, make explicit assignments to pointer instance variables for retained objects.

Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.

If a subclass inherits NSCopying from its superclass and declares additional instance variables, the subclass has to override copyWithZone: to properly handle its own instance variables, invoking the superclass’s implementation first.

And the copyWithZone: documentation:

Returns a new instance that’s a copy of the receiver.

- (id)copyWithZone:(NSZone *)zone

Parameters

zone
The zone identifies an area of memory from which to allocate for the new instance. If zone is NULL, the new instance is allocated from the default zone, which is returned from the function NSDefaultMallocZone.

Discussion

The returned object is implicitly retained by the sender, who is responsible for releasing it. The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

Eraserhead
Sep 28, 2006, 03:43 PM
Thanks, I've just found that documentation, good to know I'm on the right track!

Eraserhead
Sep 28, 2006, 04:00 PM
My program works too! Excellent! :D