0

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.

Tim
  • 8,932
  • 4
  • 43
  • 64
  • 1
    inner for has the same value "i". – Szu Jun 09 '14 at 12:49
  • Nah, it is a bad style but the indexing should work fine, the inner loop scope has `i` var from the external loop being "hidden". – A-Live Jun 09 '14 at 13:05
  • @Szu I changed the i to j in inner loop and checked the o/p the same o/p is given.In slog i got the currencies array correct.like consider here kl is 4,5,3 respectively .my o/p is. when outer loop i =0...array....[0,0,0,0],full dictionary...{key1:[0,0,0,0]}. when outer loop i=1 array....[0,0,0,0,0],full dictionary...{key1:[0,0,0,0,0], key2:[0,0,0,0,0]}.....when outer loop i=2 array....[0,0,0],full dictionary...{key1:[0,0,0], key2:[0,0,0], key3:[0,0,0]}..... – user3698427 Jun 09 '14 at 13:11
  • @user3698427 Please use Edit button below the question, it allows you to add a well-formatted information. – A-Live Jun 09 '14 at 13:47
  • @A-Live i put one more question.I don't know where is error.Would you please help me to solve this issue. http://stackoverflow.com/questions/24142583/how-to-set-object-and-keys-to-nsmutabledictionary-in-correct-order – user3698427 Jun 10 '14 at 14:28

3 Answers3

1

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
A-Live
  • 8,904
  • 2
  • 39
  • 74
  • thanks alive your answer is correct now my code is working perfect.thank you. – user3698427 Jun 09 '14 at 13:21
  • @user3698427 Don't forget the created instance is not retained by anything but dictionary you are adding it to, that might cause issues. Usage of a class variable is not needed at your method, instead declare variable at the method scope to avoid this issue. – A-Live Jun 09 '14 at 13:27
  • I put one more question.Would you please help me. http://stackoverflow.com/questions/24306138/how-to-set-activity-indicator-for-a-view-when-click-the-table-cell – user3698427 Jun 19 '14 at 12:10
1

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I am new in Objective would you please help me to rewrite the my codes – user3698427 Jun 09 '14 at 13:19
  • You need to go back to your original post and explain, clearly, what it is you are trying to do. Nobody can read your mind. What is the result you are after? – Duncan C Jun 09 '14 at 14:31
0

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Won't fix the issue, all the dict values will be the same array. – A-Live Jun 09 '14 at 13:07
  • I know both text and code is hard to read at the question, anyway the problem must be that he doesn't understand how the objects are being added to the dictionaries. – A-Live Jun 09 '14 at 13:13
  • @A-Live,@Zaph I changed the i to j in inner loop and checked the o/p the same o/p is given.In slog i got the currencies array correct.like consider here kl is 4,5,3 respectively .my o/p is. when outer loop i =0...array....[0,0,0,0],full dictionary...{key1:[0,0,0,0]}. when outer loop i=1 array....[0,0,0,0,0],full dictionary...{key1:[0,0,0,0,0], key2:[0,0,0,0,0]}.....when outer loop i=2 array....[0,0,0],full dictionary...{key1:[0,0,0], key2:[0,0,0], key3:[0,0,0]}.....i need output like..full dictionary...{key1:[0,0,0,0], key2:[0,0,0,0,0], key3:[0,0,0]} – user3698427 Jun 09 '14 at 13:14
  • @user3698427 That is because you are using the same array for all objects. Whenever possible, make edits to the question when adding details, at the comment you can point that the question was updated. – A-Live Jun 09 '14 at 13:17
  • As @A-Live states, please add additional information to your original post. – zaph Jun 09 '14 at 13:43