On Jan 24, 2016, at 08:16 , Dave <d...@looktowindward.com> wrote:
> 
> can I just do this?
> 
> myDestNetwork.pArray1 = [mySourceNetwork copy];

No. The ‘copy’ method has no intrinsic depth or shallowness. For your custom 
classes, it does what you’ve implemented it to do. For Cocoa classes, they do 
what they’re documented to do.

> Or do I need to use the initWithArray:xxxx copyItems:YES and 
> initWithDictionary:xxxx copyItems:YES methods?

No, not that either. According to the NSArray documentation, for example:

> “flag: If YES, each object in array receives a copyWithZone: message to 
> create a copy of the object”
> …
> "The copyWithZone: method performs a shallow copy. If you have a collection 
> of arbitrary depth, passing YES for the flag parameter will perform an 
> immutable copy of the first level below the surface.”

(Note that this last bit means that ‘copyWithZone:’ *sent to collection 
classes* performs a shallow copy. As I said before, in general there’s no 
absolute API contract. In other cases, it does whatever it does.)

The *easy* way to do a deep copy is in fact to archive the root object and 
unarchive the result.

Or, you can write your own deep copy mechanism. In it, you’ll have to re-invent 
parts of the archiving mechanism. In particular, you’ll probably create a 
mapping table that maps source object pointers into their copied equivalents. 
The presence of a source object in the mapping both tells you whether it’s been 
copied (allowing you to walk the object network graph without going in 
circles), and what it was copied to. You’ll likely also want some kind of 
‘copyWithMapping:’ method in each of your custom classes, paralleling the 
semantics of ‘encodeWithCoder:’.

Personally, if I had an archive+unarchive mechanism that gave the correct 
result, I’d use that in preference to writing the copying code, memory and 
performance considerations allowing.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to