遇到问题:
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"LiLei",@"NameOne",
[NSArray arrayWithObjects:@"one",@"two",nil],@"ArrayOne",
[NSArray arrayWithObjects:@"First",@"Second",nil],@"ArrayTwo", nil];
NSMutableDictionary *mutDic = [dic mutableDeepCopyOfDictionary];
// 改内置的ArrayTwo
NSMutableArray *mutArr = (NSMutableArray *)[mutDic objectForKey:@"ArrayTwo"];
[mutArr addObject:@"Third"];
NSLog(@"mutDic=%@", mutDic);
reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fe8d36911e0'
简单的解决办法,解决少量的问题
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"LiLei",@"NameOne",
[NSArray arrayWithObjects:@"one",@"two",nil],@"ArrayOne",
[NSArray arrayWithObjects:@"First",@"Second",nil],@"ArrayTwo", nil];
NSMutableDictionary *mutDic = [dic mutableCopy];
NSMutableArray *mutArr = [NSMutableArray arrayWithArray:[mutDic objectForKey:@"ArrayTwo"]];
[mutArr addObject:@"Third"];
[mutDic setValue:mutArr forKey:@"ArrayTwo"];
NSLog(@"mutDic=%@", mutDic);
结果如下:
mutDic={
ArrayOne = (
one,
two
);
ArrayTwo = (
First,
Second,
Third
);
NameOne = LiLei;
}
如果要对多个元素更改的话,建议下面的
//为字典写个分类
#import <Foundation/Foundation.h>
@interface NSDictionary (deepCopy)
//字典的深拷贝
- (NSMutableDictionary *) mutableDeepCopyOfDictionary;
@end
#import "NSDictionary+deepCopy.h"
#import "NSArray+deepCopy.h"
@implementation NSDictionary (deepCopy)
- (NSMutableDictionary *) mutableDeepCopyOfDictionary{
//实例化一个可变的字典
NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:[self count]];
//获取字典的所有键
NSArray *keys = [self allKeys];
for (id key in keys) {
//取出字典的元素
id arrayElement = [self valueForKey:key];
id elementCopy =nil;
if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfDictionary)]) {
//判断元素是否是一个实现了mutableDeepCopyOfDictionary的字典
elementCopy = [arrayElement mutableDeepCopyOfDictionary];
}else if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfArray)]) {
//判断元素是否是一个实现了mutableDeepCopyOfDictionary的数组
elementCopy = [arrayElement mutableDeepCopyOfArray] ;
}else if ([arrayElement conformsToProtocol:@protocol(NSMutableCopying)]) {
elementCopy = [arrayElement mutableCopy];
}
if (elementCopy ==nil) {
elementCopy = [arrayElement copy];
}
[newDict setObject:elementCopy forKey:key];
}
return newDict;
}
@end
//为数组写个分类
#import <Foundation/Foundation.h>
@interface NSArray (deepCopy)
//数组的深拷贝方法
-(NSMutableArray *)mutableDeepCopyOfArray;
@end
#import "NSArray+deepCopy.h"
#import "NSDictionary+deepCopy.h"
@implementation NSArray(deepCopy)
- (NSMutableArray *)mutableDeepCopyOfArray {
//穿创建一个可变数组
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:[self count]];
for (int i = 0; i < [self count]; i++) {
//取数组的一个元素
id arrayElement = [self objectAtIndex:i];
//用于接受添加的数组元素
id elementCopy =nil;
if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfArray)]) {
//判断数组元素是否是一个实现了deepCopyOfArray方法的数组
elementCopy = [arrayElement mutableDeepCopyOfArray];
}else if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfDictionary)]) {
//判断数组元素是否是一个实现了deepCopyOfArray方法的字典
elementCopy = [arrayElement mutableDeepCopyOfDictionary];
}else if ([arrayElement conformsToProtocol:@protocol(NSMutableCopying)]) {
elementCopy = [arrayElement mutableCopy];
}
if (elementCopy ==nil) {
elementCopy = [arrayElement copy];
}
[newArray addObject:elementCopy];
}
return newArray;
}
@end
#import "NSArray+deepCopy.h"
#import "NSDictionary+deepCopy.h"
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"LiLei",@"NameOne",
[NSArray arrayWithObjects:@"one",@"two",nil],@"ArrayOne",
[NSArray arrayWithObjects:@"First",@"Second",nil],@"ArrayTwo", nil];
NSMutableDictionary *mutDic = [dic mutableDeepCopyOfDictionary];
NSMutableArray *mutArr = (NSMutableArray *)[mutDic objectForKey:@"ArrayTwo"];
[mutArr addObject:@"Third"];
NSLog(@"mutDic=%@", mutDic);
测试结果如下:
mutDic={
ArrayOne = (
one,
two
);
ArrayTwo = (
First,
Second,
Third
);
NameOne = LiLei;
}