2013-06-24 26 views
0

我有兩個NSMutableDictionaries分配一個NSMutableDictiory到另一個的NSMutableDictionary給概率

// ServiceResponse是的NSMutableDictionary

 NSMutableDictionary *LocalDict=[[NSMutableDictionary alloc]initWithDictionary:serviceResponse]; 

時,我在LocalDict一些變化則ServiceResponse也changing.i認爲LocalDict走的是serviceResponse.i的參考做了許多改變,但無法解決這個問題。請告訴我如何解決這個問題。爲什麼發生在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"]]; 
        } 
       } 
      } 
+0

嘗試針對localDictionary中的鍵設置Service Response字典。 – Ashim

+2

什麼是可怕的數據結構...因爲localDictionary不是深層複製的serviceResponse,你應該讓你自己的深拷貝 – adali

+0

,但我需要與服務響應dictionary.Is任何其他方式沒有改變格式我可以做這個 ? – garry007

回答

0

嗯,我想這和它解決了我的問題。每1分鐘提供建議和幫助。

   NSData *buffer; 
        NSMutableDictionary *dictLocal; 

         // Deep copy "all" objects in _dict1 pointers and all to _dict2 
       buffer = [NSKeyedArchiver archivedDataWithRootObject: serviceResponse]; 
       dictLocal = [NSKeyedUnarchiver unarchiveObjectWithData: buffer]; 
0

試試這個

NSMutableDictionary *LocalDict=[[[NSMutableDictionary alloc]initWithDictionary:serviceResponse] mutableCopy]; 

注:它使一個淺拷貝。它不會在你的可變字典中生成不可變的對象。

希望有幫助。

+0

嗯,我試過所有這不值得充分 – garry007

+0

你應該提到你已經嘗試過,並沒有投票時有人試圖根據給出的信息來幫助你。 – Tala

+0

你不應該指責人們投降。提示:這不是他;-) –

0

這個答案可以通過深拷貝

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

解決您的問題,這是一個很好的類別

@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 
相關問題