Re: How to clone a mutable dictionary
Re: How to clone a mutable dictionary
- Subject: Re: How to clone a mutable dictionary
- From: Graham Cox <email@hidden>
- Date: Fri, 24 Apr 2009 18:49:18 +1000
On 24/04/2009, at 6:39 PM, Graham Cox wrote:
I'm sorry if this is something silly!
When a dictionary is copied, the objects it contains are not copied,
merely retained by the second dictionary. Likewise setObject:forKey
only retains the object.
You need to copy each object ("deep copy") as it is transferred to
the second dictionary.
i.e. id obj = [[newThing objectForKey:key] copy];
[newThing2 setObject:obj forKey:key];
[obj release];
Incidentally this makes a very useful basic category on NSDictionary,
one that every Cocoa programmer is likely to need sooner or later.
Here's mine:
#import "NSDictionary+DeepCopy.h"
// setting this to 1 is not equivalent to a recursive deep copy if the
items in the collection are also collections.
#define DO_IT_THE_EASY_WAY 0
@implementation NSDictionary (DeepCopy)
- (NSDictionary*) deepCopy
{
#if DO_IT_THE_EASY_WAY
return [[NSDictionary alloc] initWithDictionary:self copyItems:YES];
#else
NSMutableDictionary* copy;
NSEnumerator* iter = [self keyEnumerator];
id key, cobj;
copy = [[NSMutableDictionary alloc] init];
while(( key = [iter nextObject]))
{
cobj = [[self objectForKey:key] deepCopy];
[copy setObject:cobj forKey:key];
[cobj release];
}
return copy;
#endif
}
@end
Note that this implies that there exists a category for any object
that implements -deepCopy (I have similar for NSObject, NSArray, etc)
which thus ensures that the deep copy works no matter how deeply the
structures are nested (i.e. dictionaries in dictionaries in
dictionaries work). The use of initWithDiciotnary:copyItems: does NOT
ensure this, though it would be OK if you knew for sure that your
structure wasn't recursive.
I also use the same semantics for -deepCopy as for a normal -copy,
that is, the object returned has a retain count of 1 and isn't
autoreleased.
--Graham
_______________________________________________
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