Ios 将一个NSMutableDictionary分配给另一个NSMutableDictionary,给出问题
我有两本字典 //ServiceResponse是NSMutableDictionaryIos 将一个NSMutableDictionary分配给另一个NSMutableDictionary,给出问题,ios,nsmutabledictionary,Ios,Nsmutabledictionary,我有两本字典 //ServiceResponse是NSMutableDictionary NSMutableDictionary *LocalDict=[[NSMutableDictionary alloc]initWithDictionary:serviceResponse]; 当我在LocalDict中做了一些更改时,ServiceResponse也在更改。我认为LocalDict引用了ServiceResponse。我做了很多更改,但无法解决这个问题。请告诉我如何解决这个问题。
NSMutableDictionary *LocalDict=[[NSMutableDictionary alloc]initWithDictionary:serviceResponse];
当我在LocalDict中做了一些更改时,ServiceResponse也在更改。我认为LocalDict引用了ServiceResponse。我做了很多更改,但无法解决这个问题。请告诉我如何解决这个问题。serviceResponse中发生更改的原因
if ([[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"]!=[NSNull null]) {
if ([[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"]count]>0) {
if ([[[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]!=[NSNull null]) {
NSMutableDictionary *LocalDict=[[NSMutableDictionary alloc]initWithDictionary:serviceResponse];
for (int j=0; j< [[[[[[LocalDict valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"] count] ;j++) {
for (int i=1; i<=5; i++) {
NSURL * imageURL = [NSURL URLWithString:[[[[[[[LocalDict valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]objectAtIndex:j] valueForKey:[NSString stringWithFormat:@"ThumbNailImage%d",i]]];
if (![[imageURL absoluteString]isEqualToString:@""]) {
NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.png",[[[[[[[LocalDict valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]objectAtIndex:j] valueForKey:@"ProductID"],i]];
[imageData writeToFile:savedImagePath atomically:NO];
[[[[[[[LocalDict valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]objectAtIndex:j]setValue:savedImagePath forKey:[NSString stringWithFormat:@"ThumbNailImage%d",i]];
}
}
}
[dbManager insertProductsDetails:[[[[[LocalDict valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Products"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]];
}
}
}
/*
* Favourites details
*/
if ([[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Favourite"]!=[NSNull null]) {
if ([[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Favourite"]count]>0) {
if ([[[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Favourite"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]!=[NSNull null]) {
[dbManager insertFavouritesDetails:[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"Favourite"]];
}
}
}
/*
* Rep Details
*/
if ([[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"RepDetails"]!=[NSNull null]) {
if ([[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"RepDetails"]count]>0) {
if ([[[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"RepDetails"] objectAtIndex:0]valueForKey:@"OutputProductsDetails"]!=[NSNull null]) {
[dbManager insertRepUserDetails:[[[serviceResponse valueForKey:@"GetAllDetailsResult"]objectAtIndex:0]valueForKey:@"RepDetails"]];
}
}
}
if([[serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“Products”]!=[NSNull null]){
如果([[[serviceResponse valueForKey:@“GetAllDetailsResult”]对象索引:0]valueForKey:@“Products”]计数]>0){
如果([serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“产品”]objectAtIndex:0]valueForKey:@“OutputProductsDetails”!=[NSNull null]){
NSMutableDictionary*LocalDict=[[NSMutableDictionary alloc]initWithDictionary:serviceResponse];
对于(int j=0;j<[LocalDict valueForKey:@“GetAllDetailsResult”]对象索引:0]valueForKey:@“产品”]对象索引:0]valueForKey:@“OutputProductsDetails”]计数];j++){
对于(int i=1;i0){
如果([serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“Favorite”]objectAtIndex:0]valueForKey:@“OutputProductsDetails”!=[NSNull null]){
[dbManager插入收藏夹详细信息:[[serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“收藏夹”];
}
}
}
/*
*代表详细信息
*/
如果([[serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“RepDetails”]!=[NSNull null]){
如果([[[serviceResponse valueForKey:@“GetAllDetailsResult”]对象索引:0]valueForKey:@“RepDetails”]计数]>0){
如果([serviceResponse valueForKey:@“GetAllDetailsResult”]objectAtIndex:0]valueForKey:@“RepDetails”]objectAtIndex:0]valueForKey:@“OutputProductsDetails”!=[NSNull null]){
[dbManager insertRepUserDetails:[[serviceResponse valueForKey:@“GetAllDetailsResult”]对象索引:0]valueForKey:@“RepDetails”];
}
}
}
试试这个
NSMutableDictionary *LocalDict=[[[NSMutableDictionary alloc]initWithDictionary:serviceResponse] mutableCopy];
注意:它是一个浅拷贝。它不会在可变字典中生成不可变的对象
希望有帮助。这个答案可以通过深度复制解决您的问题 这是一个很好的分类
@implementation NSDictionary (SPDeepCopy)
- (NSDictionary*) deepCopy {
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];
if ([obj respondsToSelector:@selector(deepCopy)])
cObjects[i] = [obj deepCopy];
else
cObjects[i] = [obj copy];
if ([thisKey respondsToSelector:@selector(deepCopy)])
cKeys[i] = [thisKey deepCopy];
else
cKeys[i] = [thisKey copy];
++i;
}
NSDictionary *ret = [[NSDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count] retain];
// The newly-created dictionary retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i) {
[cObjects[i] release];
[cKeys[i] release];
}
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];
// Next try to do a deep copy
else if ([obj respondsToSelector:@selector(deepCopy)])
cObjects[i] = [obj deepCopy];
// If all else fails, fall back to an ordinary copy
else
cObjects[i] = [obj copy];
// 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
cKeys[i] = [thisKey copy];
++i;
}
NSMutableDictionary *ret = [[NSMutableDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count] retain];
// The newly-created dictionary retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i) {
[cObjects[i] release];
[cKeys[i] release];
}
return ret;
}
@end
@implementation-NSDictionary(SPDeepCopy)
-(NSDictionary*)深度复制{
无符号整数计数=[自计数];
id对象[计数];
id cKeys[计数];
N分子*e=[自键枚举器];
无符号整数i=0;
id此键;
而((thisKey=[e nextObject])!=nil){
id obj=[self-objectForKey:thisKey];
if([obj respondsToSelector:@selector(deepCopy)])
cObjects[i]=[obj deepCopy];
其他的
cObjects[i]=[obj copy];
如果([此键响应选择器:@selector(deepCopy)])
cKeys[i]=[thisKey deepCopy];
其他的
cKeys[i]=[此密钥副本];
++一,;
}
NSDictionary*ret=[[NSDictionary Dictionary WithObjects:cObjects-forKeys:cKeys-count:count]retain];
//新创建的词典保留了这些,所以现在我们需要平衡上述副本
for(无符号整数i=0;i
我尝试了这个,它解决了我的问题。谢谢大家的建议和帮助
NSData *buffer;
NSMutableDictionary *dictLocal;
// Deep copy "all" objects in _dict1 pointers and all to _dict2
buffer = [NSKeyedArchiver archivedDataWithRootObject: serviceResponse];
dictLocal = [NSKeyedUnarchiver unarchiveObjectWithData: buffer];
尝试根据localDictionary中的键设置服务响应字典。多么糟糕的数据结构…因为localDictionary不是serviceResponse的深度副本,所以您应该创建自己的深度副本,但我需要与服务响应字典相同的格式。如果不更改格式,是否有其他方法可以做到这一点uld已经提到了你已经尝试过的内容,并且在有人根据提供的信息试图帮助你时,你不应该指责别人投了反对票。提示:不是他;-)好吧,我错了。只是希望看看答案有什么问题,这样我们都能从中受益。
[dict mutableCopy]