Permalink
Browse files

Deep copy categories for NSArray, NSSet, and NSDictionary.

  • Loading branch information...
uliwitness committed Feb 23, 2010
1 parent b7b2d49 commit 24dca8dfd0981e4d64a14447574730ab47bab998
Showing with 327 additions and 0 deletions.
  1. +34 −0 NSArray+DeepCopy.h
  2. +74 −0 NSArray+DeepCopy.m
  3. +34 −0 NSDictionary+DeepCopy.h
  4. +77 −0 NSDictionary+DeepCopy.m
  5. +34 −0 NSSet+DeepCopy.h
  6. +74 −0 NSSet+DeepCopy.m
View
@@ -0,0 +1,34 @@
+//
+// NSArray+DeepCopy.h
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+/*
+ This category adds a method to NSArray that performs a deep copy of all
+ instead of the usual shallow copy. For any objects inside it, it calls deepCopy
+ again, unless they don't implement that selector, then a regular copy will
+ be done.
+*/
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import <Foundation/Foundation.h>
+
+
+// -----------------------------------------------------------------------------
+// Interface:
+// -----------------------------------------------------------------------------
+
+@interface NSArray (UKDeepCopy)
+
+-(NSArray*) deepCopy; // Call deepCopy on anything that understands it, copy on anything else.
+
+-(NSMutableArray*) deepMutableContainerCopy; // Call deepMutableContainerCopy on anyone that understands it, copy on anything else.
+-(NSMutableArray*) deepMutableCopy; // Call deepMutableCopy on anyone that understands it, mutableCopy on anything else.
+
+@end
View
@@ -0,0 +1,74 @@
+//
+// NSArray+DeepCopy.m
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import "NSArray+DeepCopy.h"
+
+
+@implementation NSArray (UKDeepCopy)
+
+-(NSArray*) deepCopy
+{
+ NSMutableArray * theArray = [NSMutableArray arrayWithCapacity: [self count]];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theArray addObject: currObj];
+ }
+
+ return theArray;
+}
+
+
+-(NSMutableArray*) deepMutableContainerCopy
+{
+ NSMutableArray * theArray = [NSMutableArray array];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableContainerCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theArray addObject: currObj];
+ }
+
+ return theArray;
+}
+
+
+-(NSMutableArray*) deepMutableCopy
+{
+ NSMutableArray * theArray = [NSMutableArray array];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableCopy] autorelease];
+ else
+ currObj = [[currObj mutableCopy] autorelease];
+ [theArray addObject: currObj];
+ }
+
+ return theArray;
+}
+
+@end
View
@@ -0,0 +1,34 @@
+//
+// NSDictionary+DeepCopy.h
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+/*
+ This category adds a method to NSDictionary that performs a deep copy of all
+ instead of the usual shallow copy. For any objects inside it, it calls deepCopy
+ again, unless they don't implement that selector, then a regular copy will
+ be done.
+*/
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import <Foundation/Foundation.h>
+
+
+// -----------------------------------------------------------------------------
+// Interface:
+// -----------------------------------------------------------------------------
+
+@interface NSDictionary (UKDeepCopy)
+
+-(NSDictionary*) deepCopy; // Call deepCopy on anything that understands it, copy on anything else.
+
+-(NSMutableDictionary*) deepMutableContainerCopy; // Call deepMutableContainerCopy on anyone that understands it, copy on anything else.
+-(NSMutableDictionary*) deepMutableCopy; // Call deepMutableCopy on anyone that understands it, mutableCopy on anything else.
+
+@end
View
@@ -0,0 +1,77 @@
+//
+// NSDictionary+DeepCopy.m
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import "NSDictionary+DeepCopy.h"
+
+
+@implementation NSDictionary (UKDeepCopy)
+
+-(NSDictionary*) deepCopy
+{
+ NSMutableDictionary * theDict = [NSMutableDictionary dictionaryWithCapacity: [self count]];
+ NSEnumerator * enny = [self keyEnumerator];
+ NSString * currKey = nil;
+
+ while(( currKey = [enny nextObject] ))
+ {
+ id currObj = [self objectForKey: currKey];
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theDict setObject: currObj forKey: currKey];
+ }
+
+ return theDict;
+}
+
+
+-(NSMutableDictionary*) deepMutableContainerCopy
+{
+ NSMutableDictionary * theDict = [NSMutableDictionary dictionary];
+ NSEnumerator * enny = [self keyEnumerator];
+ NSString * currKey = nil;
+
+ while(( currKey = [enny nextObject] ))
+ {
+ id currObj = [self objectForKey: currKey];
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableContainerCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theDict setObject: currObj forKey: currKey];
+ }
+
+ return theDict;
+}
+
+
+-(NSMutableDictionary*) deepMutableCopy
+{
+ NSMutableDictionary * theDict = [NSMutableDictionary dictionary];
+ NSEnumerator * enny = [self keyEnumerator];
+ NSString * currKey = nil;
+
+ while(( currKey = [enny nextObject] ))
+ {
+ id currObj = [self objectForKey: currKey];
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableCopy] autorelease];
+ else
+ currObj = [[currObj mutableCopy] autorelease];
+ [theDict setObject: currObj forKey: currKey];
+ }
+
+ return theDict;
+}
+
+@end
View
@@ -0,0 +1,34 @@
+//
+// NSSet+DeepCopy.h
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+/*
+ This category adds a method to NSSet that performs a deep copy of all
+ instead of the usual shallow copy. For any objects inside it, it calls deepCopy
+ again, unless they don't implement that selector, then a regular copy will
+ be done.
+*/
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import <Foundation/Foundation.h>
+
+
+// -----------------------------------------------------------------------------
+// Interface:
+// -----------------------------------------------------------------------------
+
+@interface NSSet (UKDeepCopy)
+
+-(NSSet*) deepCopy; // Call deepCopy on anything that understands it, copy on anything else.
+
+-(NSMutableSet*) deepMutableContainerCopy; // Call deepMutableContainerCopy on anyone that understands it, copy on anything else.
+-(NSMutableSet*) deepMutableCopy; // Call deepMutableCopy on anyone that understands it, mutableCopy on anything else.
+
+@end
View
@@ -0,0 +1,74 @@
+//
+// NSSet+DeepCopy.m
+// UKSyntaxColoredDocument
+//
+// Created by Uli Kusterer on Tue May 18 2004.
+// Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
+//
+
+// -----------------------------------------------------------------------------
+// Headers:
+// -----------------------------------------------------------------------------
+
+#import "NSSet+DeepCopy.h"
+
+
+@implementation NSSet (UKDeepCopy)
+
+-(NSSet*) deepCopy
+{
+ NSMutableSet * theSet = [NSMutableSet setWithCapacity: [self count]];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theSet addObject: currObj];
+ }
+
+ return theSet;
+}
+
+
+-(NSMutableSet*) deepMutableContainerCopy
+{
+ NSMutableSet * theSet = [NSMutableSet set];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableContainerCopy] autorelease];
+ else
+ currObj = [[currObj copy] autorelease];
+ [theSet addObject: currObj];
+ }
+
+ return theSet;
+}
+
+
+-(NSMutableSet*) deepMutableCopy
+{
+ NSMutableSet * theSet = [NSMutableSet set];
+ NSEnumerator * enny = [self objectEnumerator];
+ id currObj = nil;
+
+ while(( currObj = [enny nextObject] ))
+ {
+ if( [currObj respondsToSelector: @selector(deepCopy)] )
+ currObj = [[currObj deepMutableCopy] autorelease];
+ else
+ currObj = [[currObj mutableCopy] autorelease];
+ [theSet addObject: currObj];
+ }
+
+ return theSet;
+}
+
+@end

0 comments on commit 24dca8d

Please sign in to comment.