2

I've a NSArray of MyObjects.

I want to duplicate the last object of my array. In other terms, I want to add a new object to the array that's exactly the same of the last one.

I tried with:

id object = [[self arrangedObjects] lastObject]; 
id newObject = [object copy];

But I've realized that I can't use copy method, because it is not implemented in NSObject.

Should I implement the copy method in MyObject class, returning a new instance of MyObject and change the code as follows:

MyObject object = [[self arrangedObjects] lastObject]; 
MyObject newObject = [object copy];

Or are there other solutoins ?

UPDATE From NSObject documentation

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.

James Webster
  • 31,873
  • 11
  • 70
  • 114
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

2 Answers2

1

If your objects are NSObject, you can make a new NSObject category that implements the NSCopying Protocol. Be sure to iterate through all keys and copy their value to your new object. For deep copying you should also call 'copy' on each of its value objects.

If your object is custom, implement the protocol in it's class.

If you have mixed objects in the array, all of them must implement NSCopying protocol therefor you can use id<NSCopying> when you declare them to avoid a compile time warning/error.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
0

As far as I know, if you want to copy an object, implement the NSCopying protocol, which lets you use NSObject - (id)copy method.

In the - (id)copyWithZone:(NSZone *)zone method, allocate a new object using + (id)allocWithZone:(NSZone *)zone then copy its members, update its states, and so on

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • It says the selector copy is not recognized. And I've found this in the NSObject copy documentation: 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. – aneuryzm Sep 26 '11 at 07:53
  • The documentation says : An exception is raised if there is no implementation for copyWithZone:. That must be your unrecognized selector :) NSObject must do additional verification – Jaffa Sep 26 '11 at 07:56
  • Answering my question.. I can't use copy with NSObject, but I need to subclass it and use MyObject class where I implement NSCopying protocol. Correct ? – aneuryzm Sep 26 '11 at 08:06
  • Yes, implement NSCopying protocol in MyObject class, and it will be copyable :) – Jaffa Sep 26 '11 at 08:08
  • And what if there are objects from different classes in my array and I need to use "id" ? How do I copy the last object in this case ? – aneuryzm Sep 26 '11 at 08:11
  • As the copy method is defined in NSObject, you can call copy on it. An exception will be raised if the object is not implementing copyWithZone. Instead of using id, just use NSObject* for example :) – Jaffa Sep 26 '11 at 08:13