Dictionaries doesn't do deepCopy, but they do support NSCoding.
Do you think you can use serialization to a NSData (a memory block) instead? It 
will have other benefit when you have too much undo items and want to page to a 
file. ;)

-Jonny


在 2012-7-23,下午9:12,Andreas Grosam <[email protected]> 写道:

> 
> On 23.07.2012, at 14:04, Mike Abdullah wrote:
> 
>> 
>> On 23 Jul 2012, at 12:57, Andreas Grosam <[email protected]> wrote:
>> 
>>> 
>>> On 23.07.2012, at 13:09, Mike Abdullah wrote:
>>> 
>>>> Aha, now we're talking. You want -initWithDictionary:copyItems:
>>> 
>>> No, this is not a deep copy. 
>>> 
>>> Suppose, there is an element within the array, whose value is a mutable 
>>> dictionary. If I change the that mutable dictionary (or its values), the 
>>> copy and the dictionary would still compare equal to the copy (since 
>>> children haven't been copied).
>>> 
>>> 
>>> Thus, I need to define a deepCopy method for NSDictionary (and NSArray, 
>>> too).
>> 
>> You should seriously consider whether your design is a good idea then. 
>> Copying the objects in an array is rare, but justifiable. Having multiple 
>> levels of nested dictionaries is rarely so. Sure you're not (ab)using 
>> dictionaries to avoid defining a new class?
> 
> No, I don't think so. The NSDictionary/NSMutableDictionary is completely 
> suitable for the purpose to represent that object which I have here. There is 
> just no method to create a deep copy. (On the other hand, the isEqual method 
> will effectively  "deep"  compare.)
> 
> So, you might wonder why there is a need to have a deep copy? Well, how can I 
> answer this without digressing? ;)
> 
> But as an example:
> 
> Suppose, there is a quite rich object represented as JSON. The JSON will be 
> parsed and represented as a Foundation object, which is a NSDictionary. This 
> is my object. Now suppose, there is the possibility to edit and make changes 
> to this deep leveled object. Before making any changes in a GUI, I need to 
> create a "Momento" (see, wiki <http://en.wikipedia.org/wiki/Memento_pattern> 
> in order to save state of the original object and possibly be able to revert 
> changes.
> 
> Due to the lack of attributes attachable to properties in Objective-C 
> language and a momento method (I already warned that I'll possibly digressing 
> ;)  ) I just use a deep copy for creating the momento.
> 
> Sure, I could use CoreData for accomplishing this as well …  but this was 
> just an example.
> 
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list ([email protected])
> 
> 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/yingshen.yu%40gmail.com
> 
> This email sent to [email protected]

_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [email protected]

Reply via email to