fast enumeration for NSDictionary instance ordered by the key

≡放荡痞女 提交于 2019-12-12 11:34:24

问题


Overview

  • I am using fast enumeration to iterate through an NSDictionary instance
  • I expected the NSDictionary instance to be enumerated based on the ascending order of the key but that doesn't seem to be the case

What I want to do:

  • I want to be able iterate through the NSDictionary instance in the ascending order of the key using fast enumeration

Note: Pls see expected output vs actual output

Questions

  1. Am i making a mistake with my implementation ?
  2. Does NSDictionary's fast enumeration guarantee ordering based on keys ?
  3. If not then is there a work around for this and yet use fast enumeration ?

Example

#import<Foundation/Foundation.h>

int main()
{
    system("clear");

    NSDictionary *d1 = nil;

    @autoreleasepool
    {   

        d1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"AAA", [NSNumber numberWithInt:10], 
              @"BBB", [NSNumber numberWithInt:20],               
              @"CCC", [NSNumber numberWithInt:30],               
              nil];
    }   

    for(NSNumber* n1 in d1)     //I expected fast enumeration for NSDictionary to be based on the 
        //ascending order of the key but that doesn't seem to be the case
    {
        printf("key = %p"
               "\t [key intValue] = %i"
               "\t value = %s\n", 
               n1, 
               [n1 intValue], 
               [[d1 objectForKey:n1] UTF8String]);
    }   

    return(0);
}

Expected Output

key = 0xa83      [key intValue] = 10     value = AAA
key = 0x1483     [key intValue] = 20     value = BBB
key = 0x1e83     [key intValue] = 30     value = CCC

Actual Output

key = 0x1e83     [key intValue] = 30     value = CCC
key = 0xa83      [key intValue] = 10     value = AAA
key = 0x1483     [key intValue] = 20     value = BBB

回答1:


  1. No your implementation is correct.
  2. NSDictionary fast enumeration does not guarantee sorting (and it will not output anything in order because of implementation as hashed container).
  3. No, you have to sort it yourself.



回答2:


for (NSString *key in [[d1 allKeys] sortedArrayUsingSelector:@selector(compare:)])
{
    id value = [d1 valueForKey:key];
    ...
}



回答3:


There is no guaranties about the order in which you will receive your object.

allKeys
Returns a new array containing the dictionary’s keys.
- (NSArray *)allKeys
Return Value
A new array containing the dictionary’s keys, or an empty array if the dictionary has no entries.
Discussion
The order of the elements in the array is not defined.

So my suggestion is, if your dictionary doesn't change often, cache an NSArray with the key in the order you want them.
If your dictionary often change, you may have to sort allKeys when you need them.



来源:https://stackoverflow.com/questions/8529660/fast-enumeration-for-nsdictionary-instance-ordered-by-the-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!