IOS深度拷貝,NSArray,NSDictionary的分類(Category)

來源:互聯網
上載者:User

標籤:blog   io   ar   os   for   sp   檔案   on   2014   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.