Skip to content

Instantly share code, notes, and snippets.

@yfujiki
Created January 23, 2012 18:48
Show Gist options
  • Save yfujiki/1664847 to your computer and use it in GitHub Desktop.
Save yfujiki/1664847 to your computer and use it in GitHub Desktop.
Deep mutable copy category method of NSDictionary (ObjC)
- (NSMutableDictionary *) mutableDeepCopy {
NSMutableDictionary * returnDict = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSArray * keys = [self allKeys];
for(id key in keys) {
id oneValue = [self objectForKey:key];
id oneCopy = nil;
if([oneValue respondsToSelector:@selector(mutableDeepCopy)]) {
oneCopy = [oneValue mutableDeepCopy];
}
else if([oneValue respondsToSelector:@selector(mutableCopy)]) {
oneCopy = [oneValue mutableCopy];
}
else {
oneCopy = [oneValue copy];
}
[returnDict setValue:oneValue forKey:key];
}
return returnDict;
}
@Motti-Shneor
Copy link

This is so nice! Could it be even nicer, to support the equivalent of mutability-options in NSPropertyListSerialization? i.e,

typedef NS_OPTIONS(NSUInteger, NSPropertyListMutabilityOptions) {
NSPropertyListImmutable = kCFPropertyListImmutable,
NSPropertyListMutableContainers = kCFPropertyListMutableContainers,
NSPropertyListMutableContainersAndLeaves = kCFPropertyListMutableContainersAndLeaves
};

so mutableDeepCopy method can receive the option, and selectively make (deep) mutable copies only to containers, but NOT to the "leaves" (e.g. NSData, NSString, NSDate, NSNumber etc. ? What's the best way to go about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment