• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: DeepCopy Arrays and Dictionaries
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DeepCopy Arrays and Dictionaries


  • Subject: Re: DeepCopy Arrays and Dictionaries
  • From: Quincey Morris <email@hidden>
  • Date: Sun, 24 Jan 2016 10:44:50 -0800
  • Feedback-id: 167118m:167118agrif8a:167118seDG2fgDDA:SMTPCORP

On Jan 24, 2016, at 08:16 , Dave <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: DeepCopy Arrays and Dictionaries
      • From: Dave <email@hidden>
References: 
 >How to save a Dictionary Network to a plist file? (From: Dave <email@hidden>)
 >Re: How to save a Dictionary Network to a plist file? (From: email@hidden)
 >Re: How to save a Dictionary Network to a plist file? (From: Jens Alfke <email@hidden>)
 >DeepCopy Arrays and Dictionaries (From: Dave <email@hidden>)

  • Prev by Date: Re: NSTextFields will not fully justify in 10.11
  • Next by Date: Re: How to save a Dictionary Network to a plist file?
  • Previous by thread: DeepCopy Arrays and Dictionaries
  • Next by thread: Re: DeepCopy Arrays and Dictionaries
  • Index(es):
    • Date
    • Thread