Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialagreatdaytocode
24,757 PointsOrder of Key Value
We add the follow Key Value in the video:
NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"The Missing Code",@"title",@"Aaron A",@"author", nil];
However the order in the log prints the following:
2014-02-09 07:39:34.982 BlogReader[2647:70b] {
author = "Aaron A";
title = "The Missing Code";
}
Why does it print author then title? and not title then author?
4 Answers
harishupadhyayula
32,221 PointsIt really shouldn't matter which order it's getting printed. Since you always be accessing the values by it's keys. May be dictionaryWithObjectsAndKeys sorts the order by 'keys'.
RASHU BHATNAGAR
Courses Plus Student 2,669 PointsIt will always print in the alphabetical order. I experimented a little bit and did like this: NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"The Missing Code",@"title",@"Aaron A",@"author",@"The Missing Code",@"title",@"google.com",@"url" , @"writing",@"hobby",nil]; NSLog(@"%@",blogPost1);
the output which i got is :
{ author = "Aaron A"; hobby = writing; title = "The Missing Code"; url = "google.com"; }
I hope it answers your "WHY????"
agreatdaytocode
24,757 PointsAwesome thanks for the feed back thats good to know!
eirikvaa
18,015 PointsI did some testing and you are correct - thank you!
agreatdaytocode
24,757 PointsI agree, it does not matter. I just wanted to know 'why'. Thanks for replying!
eirikvaa
18,015 PointsThe entries in a NSDictionary could be of any order (the NSDictionary class doesn't keep track of the order), so don't rely on them being in the same order as you created them. Then again, as Harish Upadhyayula mentioned above, it doesn't matter which order it is since you're accessing by it's keys. If you want to order them, add them to an array and then sort them.
agreatdaytocode
24,757 PointsYeah, I understand, I was just curious on how the log worked thats all.
eirikvaa
18,015 PointsYeah, just wanted to add some technical information about the NSDictionary class (since the above commenter acutally didn't answer the "why" of your question).