ObjC: NSDictonary question

Rombus

2[H]4U
Joined
Oct 12, 2001
Messages
3,285
I've started going though the Stanford Introduction to iPhone programming course and i ran into a question about NSDictionary in the ObjC Foundation classes.

Are NSDictionarys stored in any sort of order? or is it just random how the array is processed? The code i wrote is giving me technically correct output, but its just not coming out in the order i would expect.

Here is the code in question:
Code:
    // setting up Key/Value pairs (Note: Key/Value 6 entered to prove that script
    // is looking for Stanford prefix
    NSString *key1 = @"Stanford University";
    NSURL *value1 = [NSURL URLWithString:@"http://www.stanford.edu"];
    NSString *key2 = @"Apple";
    NSURL *value2 = [NSURL URLWithString:@"http://www.apple.com"];
    NSString *key3 = @"CS193P";
    NSURL *value3 = [NSURL URLWithString:@"http://cs193p.stanford.edu"];
    NSString *key4 = @"Stanford on iTunes U";
    NSURL *value4 = [NSURL URLWithString:@"http://itunes.stanford.edu"];
    NSString *key5 = @"Stanford Mall";
    NSURL *value5 = [NSURL URLWithString:@"http://stanfordshop.com"];
    NSString *key6 = @"Not Stanford";
    NSURL *value6 = [NSURL URLWithString:@"http://www.notstanford.com"];
    
    // Setup mutable dictionary
    NSMutableDictionary *urlDictionary = [NSMutableDictionary dictionary];
    
    // add the elements to the dictionary
    [urlDictionary setObject:value1 forKey:key1];
    [urlDictionary setObject:value2 forKey:key2];
    [urlDictionary setObject:value3 forKey:key3];
    [urlDictionary setObject:value4 forKey:key4];
    [urlDictionary setObject:value5 forKey:key5];
    [urlDictionary setObject:value6 forKey:key6];
    
    // Get the count of urlDictionary
    int urlDictionaryCount = [urlDictionary count];
    
    // Setup some intial values
    NSLog( @"Stanford Dictionary");
    NSLog( @"Entries in Dictionary: %i", urlDictionaryCount);
    NSLog( @"Keys that start with \"Stanford\"");
    
    // Fast enumeration using the key method
    for (id key in urlDictionary){
        if ([key hasPrefix:@"Stanford "]){
                NSLog( @"Key: '%@' URL: '%@' ", key, [urlDictionary objectForKey:key]);
        }
    }
Here is the output:
Code:
Stanford Dictionary
Entries in Dictionary: 6
Keys that start with "Stanford"
Key: 'Stanford on iTunes U' URL: 'http://itunes.stanford.edu' 
Key: 'Stanford University' URL: 'http://www.stanford.edu' 
Key: 'Stanford Mall' URL: 'http://stanfordshop.com'
I was EXPECTING (due to the order i put the values in):
Code:
Stanford Dictionary
Entries in Dictionary: 6
Keys that start with "Stanford"
Key: 'Stanford University' URL: 'http://www.stanford.edu' 
Key: 'Stanford on iTunes U' URL: 'http://itunes.stanford.edu' 
Key: 'Stanford Mall' URL: 'http://stanfordshop.com'
So am i just over looking something? It consistently has came up with the same output, so i dont think the output is random, but it also does not seem to be alphabetic or anything.
 
Yep, hashmaps do not guarantee order so the behavior you're seeing is expected. You're right, it is not random. It's implementation specific.
 
@amromusa

Cool, thanks for the information! Just curious, is there any way to have any control over that, or just take care of any ordering in further code?
 
NP. Nope, not using that data structure. The idea behind hashmaps is quick lookup. You basically give up order to get that since one hashes the key to find the location of the object it maps to. You'll need to take care of it with further code.

How to do that depends on how often you need to iterate over the data in an ordered manner. If you're doing that a lot chances are a list is a better option (NS(Mutable)Array) since it maintains order. If you're not doing a lot of that, then you can just iterate over the contents of the map, sort, and do what you need to do.
 
Back
Top