遇到問題:
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"LiLei",@"NameOne",
[NSArray arrayWithObjects:@"one",@"two",nil],@"ArrayOne",
[NSArray arrayWithObjects:@"First",@"Second",nil],@"ArrayTwo", nil];
NSMutableDictionary *mutDic = [dic mutableDeepCopyOfDictionary];
// 改內(nèi)置的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);
結(jié)果如下:
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)]) {
//判斷元素是否是一個實現(xiàn)了mutableDeepCopyOfDictionary的字典
elementCopy = [arrayElement mutableDeepCopyOfDictionary];
}else if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfArray)]) {
//判斷元素是否是一個實現(xiàn)了mutableDeepCopyOfDictionary的數(shù)組
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
//為數(shù)組寫個分類
#import <Foundation/Foundation.h>
@interface NSArray (deepCopy)
//數(shù)組的深拷貝方法
-(NSMutableArray *)mutableDeepCopyOfArray;
@end
#import "NSArray+deepCopy.h"
#import "NSDictionary+deepCopy.h"
@implementation NSArray(deepCopy)
- (NSMutableArray *)mutableDeepCopyOfArray {
//穿創(chuàng)建一個可變數(shù)組
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:[self count]];
for (int i = 0; i < [self count]; i++) {
//取數(shù)組的一個元素
id arrayElement = [self objectAtIndex:i];
//用于接受添加的數(shù)組元素
id elementCopy =nil;
if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfArray)]) {
//判斷數(shù)組元素是否是一個實現(xiàn)了deepCopyOfArray方法的數(shù)組
elementCopy = [arrayElement mutableDeepCopyOfArray];
}else if ([arrayElement respondsToSelector:@selector(mutableDeepCopyOfDictionary)]) {
//判斷數(shù)組元素是否是一個實現(xiàn)了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);
測試結(jié)果如下:
mutDic={
ArrayOne = (
one,
two
);
ArrayTwo = (
First,
Second,
Third
);
NameOne = LiLei;
}