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:
Here is the output:
I was EXPECTING (due to the order i put the values in):
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.
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]);
}
}
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'
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'