-1

I am trying set objects for particular keys to an NSMutableDictionary in a for loop

The code:

for(int k =0;k<currenyArry.count;k++)
{
    [_currenies setObject:@"0" forKey:currenyArry[k]];
}

Here, _currenies is an NSMutableDictionary and currenyArry is an NSMutableArray.

For example, currentArry is:

[1,3,5,10,100,500,1000];

After setting the objects in _currenies dictionary, it looks like:

{1:"0",10:"0",100:"0",1000:"0",3:"0",5:"0",500:"0"}

But I need the order based on my currenyArry like

{1:"0",3:"0",5:"0",10:"0",100:"0",500:"0",1000:"0"}

How can I modify my code to achieve this?

Stonz2
  • 6,306
  • 4
  • 44
  • 64

2 Answers2

2

This is the correct answer - NSDictionary and NSMutableDictionary are hash-based containers, which are therefore unordered.

To get your data from NSDictionary in a specific order, you can order the keys, and then pull the data from the container in the order that you want:

for (NSNumber *key in currenyArry) {
    NSLog(@"Key: %@ Value: %@", key, _currenies[key]);
}

This will produce the key-value pairs in the order defined by teh currenyArray. Of course your code can do any other processing as needed, rather than simply printing key-value pairs.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • But its setting in some order as i shown in my example.but i need the correct order of fool loop – user3698427 Jun 10 '14 at 13:50
  • @user3698427 You can iterate your `currentArray` and use its values as keys into `NSMutableDictionary` then. There is absolutely, positively, no way to make `NSMutableDictionary` to iterate internally in a predictable order because of its very nature of a hash-based container. If you want a specific order, you need to establish it externally to the dictionary being iterated - through an array that you have, by sorting keys the way the answer shows, or in some other way that you find convenient, but the ordering needs to be done outside the container. – Sergey Kalinichenko Jun 10 '14 at 13:56
  • I think you understand my scenario would please help me to rewrite the code i mentioned in question – user3698427 Jun 10 '14 at 14:01
  • 2
    @user3698427 you need to understand that the order you are adding the values doesn't matter, the keys and values order is arbitrary and dictionary is only saving relation between keys and values. If you want to read the values based on some order of the keys, get an array of all keys of the dict, sort it as you wish and use sorted keys array to access the values. To avoid bugs don't use global arrays to save your dicts keys, `NSDictionary` has a property to get all of them: `allKeys`. – A-Live Jun 10 '14 at 14:57
0

You can try my code:

NSArray *currentArry = @[@1,@3,@5,@10,@100,@500,@1000];
NSMutableDictionary *dict = [NSMutableDictionary new];
[currentArry enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    [dict setObject:@0 forKey:obj];
}];

NSArray * sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)];

[sortedKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@:%@",obj,dict[obj]);
}];

You didn't need to sorted dictionary - all you need it sorted keys, for getting data by this key.

mvadim
  • 152
  • 6