Created
January 23, 2012 18:48
-
-
Save yfujiki/1664847 to your computer and use it in GitHub Desktop.
Deep mutable copy category method of NSDictionary (ObjC)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?