Objective C Tutorial - Objective C Dictionary






Accessing All Items in a Dictionary

#import <Foundation/Foundation.h>
 
int main ()
{

   NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"75" forKey: @"XML"];
    [myDictionary setObject: @"42" forKey: @"Java"];
    [myDictionary setObject: @"09" forKey: @"SQL"];
  
    id myObject;
    for (myObject in myDictionary)
    {
        NSLog (@"Key = %@", myObject);
        NSLog (@"Value = %@", [myDictionary objectForKey: myObject]);
        NSLog (@"*****");
    }
   return 0;
}

The code above generates the following result.





Copying a Dictionary

#import <Foundation/Foundation.h>
 
int main ()
{
  NSDictionary *staticDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Data1", @"Key1", @"Data2", @"Key2", nil];
    NSLog (@"Number of items in NSDictionary = %i", [staticDictionary count]);
  
    NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
    [newDict addEntriesFromDictionary: staticDictionary];
    NSLog (@"Count in new Dictionary = %i", [newDict count]);
 
   return 0;
}

The code above generates the following result.





Copying Data Dictionary into an Array

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"555-1212" forKey: @"Jack"];
    [myDictionary setObject: @"555-5555" forKey: @"Tom"];
    [myDictionary setObject: @"555-9999" forKey: @"Jane"];    
    NSArray *myArray = [myDictionary allKeys];
    int i;
    for (i = 0; i < [myDictionary count]; i++)
    {
        NSLog (@"Key %i = %@", i, [myArray objectAtIndex:i]);
    }
    
    NSArray *secondArray = [myDictionary allValues];
    for (i = 0; i < [myDictionary count]; i++)
    {
        NSLog (@"Value %i = %@", i, [secondArray objectAtIndex:i]);
    }  
 
   return 0;
}

The code above generates the following result.

Counting Items in a Dictionary

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"555-1212" forKey: @"Jack"];
    [myDictionary setObject: @"555-9999" forKey: @"Jane"];
    [myDictionary setObject: @"555-5555" forKey: @"Tom"];
    
    int counter;
    counter = [myDictionary count];
    NSLog (@"Number of items = %i", counter);
 
   return 0;
}

The code above generates the following result.

Deleting Data from a Dictionary

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"555-1212" forKey: @"Jack"];
    [myDictionary setObject: @"555-9999" forKey: @"Jane"];
    [myDictionary setObject: @"555-5555" forKey: @"Tom"];
    
    int counter;
    counter = [myDictionary count];
    NSLog (@"Number of items = %i", counter);
  
    NSString *myString;
    myString = [myDictionary objectForKey: @"Jane"];
    NSLog (@"Jane is associated with %@", myString);
    
    [myDictionary removeObjectForKey: @"Jack"];
    NSLog (@"Number of items = %i", [myDictionary count]);
    
    [myDictionary removeAllObjects];
    NSLog (@"Number of items = %i", [myDictionary count]);
 
   return 0;
}

Retrieving an Item from a Dictionary

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"555-1212" forKey: @"John Doe"];
    [myDictionary setObject: @"555-9999" forKey: @"Al Jones"];
    [myDictionary setObject: @"555-5555" forKey: @"Mary Smith"];
    
    int counter;
    counter = [myDictionary count];
    NSLog (@"Number of items = %i", counter);
  
    NSString *myString;
    myString = [myDictionary objectForKey: @"Al Jones"];
    NSLog (@"Al Jones is associated with %@", myString);
 
   return 0;
}

Sorting Keys

#import <Foundation/Foundation.h>
 
int main ()
{
  NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
    [myDictionary setObject: @"75" forKey: @"John Doe"];
    [myDictionary setObject: @"42" forKey: @"Mary Smith"];
    [myDictionary setObject: @"09" forKey: @"Al Jones"];
    
    NSArray *sortedKeysArray = [myDictionary keysSortedByValueUsingSelector:@selector(compare:)];    
    int i;
    for (i = 0; i < [sortedKeysArray count]; i++)
    {
        NSLog (@"Array element %i = %@", i, [sortedKeysArray objectAtIndex:i]);
    }
 
   return 0;
}