diff options
Diffstat (limited to 'Source/NSDictionary_DeepMutableCopy.m')
-rw-r--r-- | Source/NSDictionary_DeepMutableCopy.m | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Source/NSDictionary_DeepMutableCopy.m b/Source/NSDictionary_DeepMutableCopy.m new file mode 100644 index 00000000..a5a49333 --- /dev/null +++ b/Source/NSDictionary_DeepMutableCopy.m @@ -0,0 +1,43 @@ +// +// NSDictionary_DeepMutableCopy.m +// +// Created by Matt Gemmell on 02/05/2008. +// Copyright 2008 Instinctive Code. All rights reserved. +// + +#import "NSDictionary_DeepMutableCopy.h" + + +@implementation NSDictionary (DeepMutableCopy) + + +- (NSMutableDictionary *)deepMutableCopy; +{ + NSMutableDictionary *newDictionary; + NSEnumerator *keyEnumerator; + id anObject; + id aKey; + + newDictionary = [self mutableCopy]; + // Run through the new dictionary and replace any objects that respond to -deepMutableCopy or -mutableCopy with copies. + keyEnumerator = [[newDictionary allKeys] objectEnumerator]; + while ((aKey = [keyEnumerator nextObject])) { + anObject = [newDictionary objectForKey:aKey]; + if ([anObject respondsToSelector:@selector(deepMutableCopy)]) { + anObject = [anObject deepMutableCopy]; + [newDictionary setObject:anObject forKey:aKey]; + [anObject release]; + } else if ([anObject respondsToSelector:@selector(mutableCopyWithZone:)]) { + anObject = [anObject mutableCopyWithZone:nil]; + [newDictionary setObject:anObject forKey:aKey]; + [anObject release]; + } else { + [newDictionary setObject:anObject forKey:aKey]; + } + } + + return newDictionary; +} + + +@end |