user3698427
user3698427

Reputation: 1

NSMutable dictionary not working properly

I am developing an iPad application and for this application I have one function as below :-

-(void)testcurrentest:(NSMutableDictionary *)keydictionary{
     NSArray *allKeys = [keydictionary allKeys];
     if ([allKeys count] > 0) {
         for(int i = 0;i< allKeys.count;i++){
             [_currenies removeAllObjects];
             NSString *product  = [NSString stringWithFormat:@"%@", [keydictionary objectForKey:allKeys[i]]];
             int kl = [productPriceSeasonCode intValue];
             for(int i =0;i<kl;i++){
                   [_currenies addObject:@"0"];

             }
             NSLog(@"................%@",_currenies);
             [_currencydictionary1 setObject:_currenies forKey:allKeys[i]];
             NSLog(@"full dictionary...%@",_currencydictionary1);
          }
     }
}

Here, NSLog print the currencies array based on the kl integer values but when I'm trying to set the NSMutableDictionary the currencies but mutable array always show the latest array values.

Upvotes: 0

Views: 85

Answers (3)

Duncan C
Duncan C

Reputation: 131418

Your code is a garbled mess. As others have pointed out, you are using the same loop index, i, in 2 nested loops, making it very hard to tell your intent. Don't do that, ever. It's horrible programming style.

You are also creating a string "product" that you never use, and fetching the same integer value of productPriceSeasonCode on every pass through the outer loop. I suspect you meant to fetch a value that varies with each entry in your keydictionary.

Then, you have an array, _currenies, which you empty on each pass through your outer loop. You then add a number of "0" strings to it, set a key/value pair in your _currencydictionary1 dictionary to the contents of that array, and then repeat. Since you re-use your _currenies array each time, every key/value pair you create in your _currencydictionary1 dictionary points to the exact same array, which you keep changing. At the last iteration of your outer loop, all the entries in your _currencydictionary1 will point to your _currenies array, which will contain the last set of contents you put there.

Create a new array for each pass through your outer array, and add that newly created array to your _currencydictionary1. You want a unique array in each key/value pair of your _currencydictionary1.

In short, NSMutableDictionary is working just fine. It's your code that isn't working properly.

Upvotes: 1

A-Live
A-Live

Reputation: 8944

You are using the same array for all values, they should be unique objects if you don't want change of one value to affect the other values. Initialise _currenies on every loop step or use its deep copy when preparing a new object.

A bit of code:

[_currenies removeAllObjects]; // < The same array you've added to dict on previous loop steps

Creating a new array at each loop step would create a unique object for all key-value pair:

_currenies = [NSMutableArray array]; // < Note it is not retained, apply memory management depending on your project configuration

Upvotes: 1

zaph
zaph

Reputation: 112857

Not an answer but comments don't have formatting.
The question should provide more information on the input and desired output.

First simplify your code and it should be easier to find the error:

-(void)testcurrentest:(NSMutableDictionary *)keydictionary{
    NSArray *allKeys = [keydictionary allKeys];
    for(NSString *key in allKeys) {
        [_currenies removeAllObjects];
        int kl = [productPriceSeasonCode intValue];
        for(int i =0; i<kl; i++){
            [_currenies addObject:@"0"];
        }
        NSLog(@"................%@",_currenies);
        _currencydictionary1[key] = _currenies;
        NSLog(@"full dictionary...%@",_currencydictionary1);
    }
}

Note: product was never used.

Upvotes: 0

Related Questions