-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutableDeepCopying.h
54 lines (50 loc) · 1.77 KB
/
MutableDeepCopying.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// https://gist.github.com/yfujiki/1664847
#import <Foundation/Foundation.h>
@protocol MutableDeepCopying <NSObject>
-(id) mutableDeepCopy;
@end
@interface NSDictionary (MutableDeepCopy) <MutableDeepCopying>
@end
@interface NSArray (MutableDeepCopy) <MutableDeepCopying>
@end
// Implementation
@implementation NSDictionary (MutableDeepCopy)
- (NSMutableDictionary *) mutableDeepCopy {
NSMutableDictionary * returnDict = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSArray * keys = [self allKeys];
for(id key in keys) {
id aValue = [self objectForKey:key];
id theCopy = nil;
if([aValue conformsToProtocol:@protocol(MutableDeepCopying)]) {
theCopy = [aValue mutableDeepCopy];
} else if([aValue conformsToProtocol:@protocol(NSMutableCopying)]) {
theCopy = [aValue mutableCopy];
} else if([aValue conformsToProtocol:@protocol(NSCopying)]){
theCopy = [aValue copy];
} else {
theCopy = aValue;
}
[returnDict setValue:theCopy forKey:key];
}
return returnDict;
}
@end
@implementation NSArray (MutableDeepCopy)
-(NSMutableArray *)mutableDeepCopy {
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:self.count];
for(id aValue in self) {
id theCopy = nil;
if([aValue conformsToProtocol:@protocol(MutableDeepCopying)]) {
theCopy = [aValue mutableDeepCopy];
} else if([aValue conformsToProtocol:@protocol(NSMutableCopying)]) {
theCopy = [aValue mutableCopy];
} else if([aValue conformsToProtocol:@protocol(NSCopying)]){
theCopy = [aValue copy];
} else {
theCopy = aValue;
}
[returnArray addObject:theCopy];
}
return returnArray;
}
@end