Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 97 lines (80 sloc) 2.866 kb
24dca8d @uliwitness Deep copy categories for NSArray, NSSet, and NSDictionary.
authored
1 //
2 // NSDictionary+DeepCopy.m
3 // UKSyntaxColoredDocument
4 //
5 // Created by Uli Kusterer on Tue May 18 2004.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright (c) 2004 M. Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
24dca8d @uliwitness Deep copy categories for NSArray, NSSet, and NSDictionary.
authored
26 //
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "NSDictionary+DeepCopy.h"
33
34
35 @implementation NSDictionary (UKDeepCopy)
36
37 -(NSDictionary*) deepCopy
38 {
2d7bd23 @uliwitness Err... adhere to Cocoa's memory management contract, don't screw the cal...
authored
39 NSMutableDictionary * theDict = [[NSMutableDictionary alloc] initWithCapacity: [self count]];
24dca8d @uliwitness Deep copy categories for NSArray, NSSet, and NSDictionary.
authored
40 NSEnumerator * enny = [self keyEnumerator];
41 NSString * currKey = nil;
42
43 while(( currKey = [enny nextObject] ))
44 {
45 id currObj = [self objectForKey: currKey];
46 if( [currObj respondsToSelector: @selector(deepCopy)] )
47 currObj = [[currObj deepCopy] autorelease];
48 else
49 currObj = [[currObj copy] autorelease];
50 [theDict setObject: currObj forKey: currKey];
51 }
52
53 return theDict;
54 }
55
56
57 -(NSMutableDictionary*) deepMutableContainerCopy
58 {
2d7bd23 @uliwitness Err... adhere to Cocoa's memory management contract, don't screw the cal...
authored
59 NSMutableDictionary * theDict = [[NSMutableDictionary alloc] init];
24dca8d @uliwitness Deep copy categories for NSArray, NSSet, and NSDictionary.
authored
60 NSEnumerator * enny = [self keyEnumerator];
61 NSString * currKey = nil;
62
63 while(( currKey = [enny nextObject] ))
64 {
65 id currObj = [self objectForKey: currKey];
66 if( [currObj respondsToSelector: @selector(deepCopy)] )
67 currObj = [[currObj deepMutableContainerCopy] autorelease];
68 else
69 currObj = [[currObj copy] autorelease];
70 [theDict setObject: currObj forKey: currKey];
71 }
72
73 return theDict;
74 }
75
76
77 -(NSMutableDictionary*) deepMutableCopy
78 {
2d7bd23 @uliwitness Err... adhere to Cocoa's memory management contract, don't screw the cal...
authored
79 NSMutableDictionary * theDict = [[NSMutableDictionary alloc] init];
24dca8d @uliwitness Deep copy categories for NSArray, NSSet, and NSDictionary.
authored
80 NSEnumerator * enny = [self keyEnumerator];
81 NSString * currKey = nil;
82
83 while(( currKey = [enny nextObject] ))
84 {
85 id currObj = [self objectForKey: currKey];
86 if( [currObj respondsToSelector: @selector(deepCopy)] )
87 currObj = [[currObj deepMutableCopy] autorelease];
88 else
89 currObj = [[currObj mutableCopy] autorelease];
90 [theDict setObject: currObj forKey: currKey];
91 }
92
93 return theDict;
94 }
95
96 @end
Something went wrong with that request. Please try again.