Categories of IOS deep copy, NSArray, and NSDictionary)
Find a place for the difference between deep copy and shallow copy ....... The bottom is the NSArray and NSDictionary deep copy classification method.
Code used for common debugging in the Code: This log will only be printed during debugging and will not appear during release.
#ifdef DEBUG#ifndef DLog# define DLog(fmt, ...) {NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}#endif#ifndef ELog# define ELog(err) {if(err) DLog(@"%@", err)}#endif#else#ifndef DLog# define DLog(...)#endif#ifndef ELog# define ELog(err)#endif#endif
The following two pieces of code are copied from the project.
SJDeepCopy. h file
#import
@interface NSArray (SJDeepCopy)- (NSArray*)deepCopy;- (NSMutableArray*) mutableDeepCopy;@end@interface NSDictionary (SJDeepCopy)- (NSDictionary*)deepCopy;- (NSMutableDictionary*)mutableDeepCopy;@end
SJDeepCopy. m file
#import "SJDeepCopy.h"@implementation NSArray (SJDeepCopy)- (NSArray*)deepCopy { NSUInteger count = [self count]; id cArray[count]; for (NSUInteger i = 0; i < count; ++i) { id obj = [self objectAtIndex:i]; if ([obj respondsToSelector:@selector(deepCopy)]) { cArray[i] = [obj deepCopy]; } else if ([obj respondsToSelector:@selector(copyWithZone:)]) { cArray[i] = [obj copy]; } else { DLog(@"********Error:NSArray DeepCopy Failed!!! ********"); return nil; } } NSArray *ret = [NSArray arrayWithObjects:cArray count:count]; return ret;}- (NSMutableArray*)mutableDeepCopy { NSUInteger count = [self count]; id cArray[count]; for (NSUInteger i = 0; i < count; ++i) { id obj = [self objectAtIndex:i]; // Try to do a deep mutable copy, if this object supports it if ([obj respondsToSelector:@selector(mutableDeepCopy)]) { cArray[i] = [obj mutableDeepCopy]; } // Then try a shallow mutable copy, if the object supports that else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) { cArray[i] = [obj mutableCopy]; } else if ([obj respondsToSelector:@selector(copyWithZone:)]) { cArray[i] = [obj copy]; } else { DLog(@"********Error:NSArray MutableDeepCopy Failed!!! ********"); return nil; } } NSMutableArray *ret = [NSMutableArray arrayWithObjects:cArray count:count]; return ret;}@end@implementation NSDictionary (SJDeepCopy)- (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 objectForKey:thisKey]; if ([obj respondsToSelector:@selector(deepCopy)]) { cObjects[i] = [obj deepCopy]; } else if([obj respondsToSelector:@selector(copyWithZone:)]) { cObjects[i] = [obj copy]; } else { DLog(@"********Error:NSDictionary DeepCopy Failed!!! ********") return nil; } if ([thisKey respondsToSelector:@selector(deepCopy)]) { cKeys[i] = [thisKey deepCopy]; } else if ([thisKey respondsToSelector:@selector(copyWithZone:)]) { cKeys[i] = [thisKey copy]; } else { DLog(@"********Error:NSDictionary Key DeepCopy Failed!!! ********") return nil; } ++i; } NSDictionary *ret = [NSDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count]; return ret;}- (NSMutableDictionary*)mutableDeepCopy { unsigned int count = [self count]; id cObjects[count]; id cKeys[count]; NSEnumerator *e = [self keyEnumerator]; unsigned int i = 0; id thisKey; while ((thisKey = [e nextObject]) != nil) { id obj = [self objectForKey:thisKey]; // Try to do a deep mutable copy, if this object supports it if ([obj respondsToSelector:@selector(mutableDeepCopy)]) { cObjects[i] = [obj mutableDeepCopy]; } // Then try a shallow mutable copy, if the object supports that else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) { cObjects[i] = [obj mutableCopy]; } else if ([obj respondsToSelector:@selector(copyWithZone:)]) { cObjects[i] = [obj copy]; } else { DLog(@"********Error:NSDictionary MutableDeepCopy Failed!!! ********") return nil; } // I don't think mutable keys make much sense, so just do an ordinary copy if ([thisKey respondsToSelector:@selector(deepCopy)]) { cKeys[i] = [thisKey deepCopy]; } else if([thisKey respondsToSelector:@selector(copyWithZone:)]) { cKeys[i] = [thisKey copy]; } else { return nil; } ++i; } NSMutableDictionary *ret = [NSMutableDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count]; return ret;}@end