首页 > 代码库 > IOS深度拷贝,NSArray,NSDictionary的分类(Category)

IOS深度拷贝,NSArray,NSDictionary的分类(Category)

深度拷贝和浅拷贝的区别就自己找个地方看下。。。。。。。最下面贴上了NSArray和NSDictionary的深度拷贝分类方法

代码中常用的调试需要用的代码:这样日志只会在调试时候打印,发布的时候并不会出现

#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

下面这两段代码都是项目中拷贝出来的,所以特意备注一下。

SJDeepCopy.h文件

#import <Foundation/Foundation.h>

@interface NSArray (SJDeepCopy)

- (NSArray*)deepCopy;
- (NSMutableArray*) mutableDeepCopy;

@end


@interface NSDictionary (SJDeepCopy)

- (NSDictionary*)deepCopy;
- (NSMutableDictionary*)mutableDeepCopy;

@end


SJDeepCopy.m文件

#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






IOS深度拷贝,NSArray,NSDictionary的分类(Category)