Skip to content

Instantly share code, notes, and snippets.

@yam-liu
Created December 8, 2014 10:55
Show Gist options
  • Save yam-liu/e998973e031fd0483854 to your computer and use it in GitHub Desktop.
Save yam-liu/e998973e031fd0483854 to your computer and use it in GitHub Desktop.
Deep Copy Categories for NSArray & NSDictionary
//
// CCDeepCopy.h
//
// Created by ooops on 12/8/14.
// Copyright (c) 2014 http://ooopscc.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#pragma mark - NSArray
@interface NSArray (CCDeepCopy)
- (NSArray *)deepCopy;
- (NSMutableArray *)mutableDeepCopy;
@end
#pragma mark - NSDictionary
@interface NSDictionary (CCDeepCopy)
- (NSDictionary *)deepCopy;
- (NSMutableDictionary *)mutableDeepCopy;
@end
//
// CCDeepCopy.m
//
// Created by ooops on 12/8/14.
// Copyright (c) 2014 http://ooopscc.com. All rights reserved.
//
#import "CCDeepCopy.h"
#pragma mark - NSArray
@implementation NSArray (CCDeepCopy)
- (NSArray *)deepCopy
{
NSUInteger count = [self count];
id cArray[count];
for (NSUInteger i = 0; i < count; i++) {
id obj = self[i];
if ([obj respondsToSelector:@selector(deepCopy)]) {
cArray[i] = [obj deepCopy];
} else {
cArray[i] = [obj copy];
}
}
return [NSArray arrayWithObjects:cArray count:count];
}
- (NSMutableArray *)mutableDeepCopy
{
NSUInteger count = [self count];
id cArray[count];
for (NSUInteger i = 0; i < count; i++) {
id obj = self[i];
if ([obj respondsToSelector:@selector(mutableDeepCopy)]) {
cArray[i] = [obj mutableDeepCopy];
} else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) {
cArray[i] = [obj mutableCopy];
} else if ([obj respondsToSelector:@selector(deepCopy)]) {
cArray[i] = [obj deepCopy];
} else {
cArray[i] = [obj copy];
}
}
return [NSMutableArray arrayWithObjects:cArray count:count];
}
@end
#pragma mark - NSDictionary
@implementation NSDictionary (CCDeepCopy)
- (NSDictionary *)deepCopy
{
NSUInteger count = [self count];
id cObjects[count];
id cKeys[count];
NSEnumerator *e = [self keyEnumerator];
NSUInteger i = 0;
id thisKey;
while ((thisKey = [e nextObject]) != nil) {
id obj = self[thisKey];
if ([obj respondsToSelector:@selector(deepCopy)]) {
cObjects[i] = [obj deepCopy];
} else {
cObjects[i] = [obj copy];
}
if ([thisKey respondsToSelector:@selector(deepCopy)]) {
cKeys[i] = [thisKey deepCopy];
} else {
cKeys[i] = [thisKey copy];
}
++i;
}
return [NSDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count];
}
- (NSMutableDictionary *)mutableDeepCopy
{
NSUInteger count = [self count];
id cObjects[count];
id cKeys[count];
NSEnumerator *e = [self keyEnumerator];
NSUInteger i = 0;
id thisKey;
while ((thisKey = [e nextObject]) != nil) {
id obj = self[thisKey];
if ([obj respondsToSelector:@selector(mutableDeepCopy)]) {
cObjects[i] = [obj mutableDeepCopy];
} else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) {
cObjects[i] = [obj mutableCopy];
} else if ([obj respondsToSelector:@selector(deepCopy)]) {
cObjects[i] = [obj deepCopy];
} else {
cObjects[i] = [obj copy];
}
if ([thisKey respondsToSelector:@selector(deepCopy)]) {
cKeys[i] = [thisKey deepCopy];
} else {
cKeys[i] = [thisKey copy];
}
++i;
}
return [NSMutableDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment