IOS深度拷貝,NSArray,NSDictionary的分類(Category) – iPhone手機開發技術文章 iPhone軟體開發教學課程

深度拷貝和淺拷貝的區別就自己找個地方看下。。。。。。。最下面貼上瞭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 

@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

發佈留言