Friday, May 6, 2011

NSDictionary example - creation.

One example of creating NSDictionary object with keys and values.
NSDictionary is a hash table with the (key, value) tuple inside.
Following code is collected from http://www.gnustep.it/nicola/Tutorials/BasicClasses/node25.html
------------------------------------------------

To create a NSDictionary, you can use the method
+dictionaryWithObjectsAndKeys:
which takes as argument a list of objects (to be considered in couples; the first one is the value, the second is the key); the list is terminated by nil. The following example creates the dictionary used as example in the previous section:
NSDictionary *dict;

dict = [NSDictionary dictionaryWithObjectsAndKeys:
               @"/opt/picture.png", @"Luca", 
               @"/home/nico/birthday.png", @"Birthday Photo", 
               @"/home/nico/birthday.png", @"Birthday Image", 
               @"/home/marghe/pic.jpg", @"My Sister", nil];
Please note the the keys follow their values rather than preceding them.