0

私は2つのNSMutableDictionariesを持っています

//ServiceResponse は NSMutableDictionary です

     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"]];
                    }
                }
            }
4

3 に答える 3

0

これを試して

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

注: 浅いコピーを作成します。可変辞書内で不変オブジェクトを作成することはありません。

それが役立つことを願っています。

于 2013-06-24T09:06:59.437 に答える
0

さて、私はこれを試してみましたが、私の問題は解決しました。every1 の提案と助けに感謝します。

                NSData *buffer;
                  NSMutableDictionary *dictLocal;

                        // Deep copy "all" objects in _dict1 pointers and all to _dict2
                buffer = [NSKeyedArchiver archivedDataWithRootObject: serviceResponse];
                dictLocal = [NSKeyedUnarchiver unarchiveObjectWithData: buffer];  
于 2013-06-25T05:13:57.650 に答える