新建一个名为“NSDdictionary-MutableDeepCopy.h”的头文件及“NSDdictionary-MutableDeepCopy.m”的类定义文件
看一下头文件中的类声明
#import <Foundation/Foundation.h> @interface NSDictionary (MutableDeepCopy) -(NSMutableDictionary *)mutableDeepCopy; @end
如下是类定义:
#import "NSDdictionary-MutableDeepCopy.h" @implementation NSDictionary (MutableDeepCopy) -(NSMutableDictionary *)mutableDeepCopy { NSMutableDictionary *ret=[NSMutableDictionary dictionaryWithCapacity:[self count] ]; //new construct NSMutableDictionary NSArray *keys=[self allKeys]; for (id key in keys) { id oneValue=[self valueForKey:key]; //old NSObject id oneCopy=nil; //new NSObject if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]) oneCopy=[oneValue mutableDeepCopy]; else if ([oneValue respondsToSelector:@selector(mutableCopy)]) oneCopy=[oneValue mutableCopy]; if (oneCopy==nil) { oneCopy=[oneValue copy]; } [ret setValue:oneCopy forKey:key]; }//end for return ret; } @end