The difference between a deep copy and a shallow copy is to find a place to look. At the bottom, the deep copy classification method of Nsarray and nsdictionary is affixed.
Code that is commonly used for debugging requires code: so that the log will only be printed during debugging, and will not appear when published
#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 all copied in the project, so we made a note.
SJDeepCopy.h file
#import <Foundation/Foundation.h> @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 does 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 is 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 does 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 is 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 does an ordinary copy if ([ThisKey resp Ondstoselector: @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
iOS deep Copy, Nsarray,nsdictionary category (category)