Objective C Tutorial - Objective C Directory






Create Dictionary

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSDictionary *dictionary1 = [[NSDictionary alloc] init];
        
        NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
        
        NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjects:listOfObjects
                                                                forKeys:listOfKeys];
        
        NSLog(@"dictionary2 = %@", dictionary2);
        
    return 0;
}

The code above generates the following result.





Reading Dictionaries

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSString *filePathName = @"/Users/Shared/dictionary.txt";
        
        NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:filePathName];
        
        NSLog(@"dictionary: %@", dictionary);
    return 0;
}

The code above generates the following result.





Saving Dictionaries

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
        
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:listOfObjects
                                                               forKeys:listOfKeys];
        
        NSString *filePathName = @"/Users/Shared/dictionary.txt";
        
        [dictionary writeToFile:filePathName
                     atomically:YES];
        
    return 0;
}

Manipulating Dictionaries

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        
        [dictionary setObject:@"Hello World"
                       forKey:@"english"];
        
        [dictionary setObject:@"Bonjour tout le monde"      forKey:@"french"];
        
        [dictionary setObject:@"Hola Mundo"
                       forKey:@"spanish"];
        
        NSLog(@"OBJECTS ADDED TO DICTIONARY: %@", dictionary);
        
        [dictionary removeObjectForKey:@"french"];
        
        NSLog(@"OBJECT REMOVED FROM DICTIONARY: %@", dictionary);
        
        [dictionary removeAllObjects];
        
        NSLog(@"ALL OBJECTS REMOVED FROM DICTIONARY: %@", dictionary);
        
    return 0;
}

The code above generates the following result.

Dictionary Iteration


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
        
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:listOfObjects
                                                               forKeys:listOfKeys];

        for (NSString *s in [dictionary allValues]) {
            NSLog(@"value: %@", s);
        }
        
        for (NSString *s in [dictionary allKeys]) {
            NSLog(@"key: %@", s);
        }
        
        [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"key = %@ and obj = %@", key, obj);
        }];
        
        
        
    return 0;
}

The code above generates the following result.

Dictionary Count


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
        
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:listOfObjects
                                                               forKeys:listOfKeys];
        NSUInteger count = dictionary.count;
        
        NSLog(@"The dictionary contains %lu items", count);
        
    return 0;
}

The code above generates the following result.