Categories of IOS deep copy, NSArray, and NSDictionary)

Source: Internet
Author: User

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






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.