O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.7. Chapter 7

A.7.1. Exercise 1 solution

After you have created the Discus project, replace the contents of the Discus.m file with the following code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Store data in an array of dictionaries
    NSArray *discs =
        [NSArray arrayWithObjects:
            [NSDictionary dictionaryWithObjectsAndKeys:
                @"CD",                      @"Type",
                [NSNumber numberWithInt:3], @"LengthInMinutes",
                @"Lounge Room",             @"Location",
                @"BB Bonkas",               @"Artist",
                nil],
            [NSDictionary dictionaryWithObjectsAndKeys:
                @"CD",                      @"Type",
                [NSNumber numberWithInt:4], @"LengthInMinutes",
                @"Attic",                   @"Location",
                @"CC Charmers",             @"Artist",
                nil],
            [NSDictionary dictionaryWithObjectsAndKeys:
                @"DVD",                         @"Type",
                [NSNumber numberWithInt:121],   @"LengthInMinutes",
@"Attic", @"Location", @"TJ Slickflick", @"Lead Actor", @"LJ Slickflick", @"Director", nil], nil]; // Extract a few entries, and print them in the console NSLog(@"The third entry in the library is a %@", [[discs objectAtIndex:2] objectForKey:@"Type"]); NSLog(@"The director is %@", [[discs objectAtIndex:2] objectForKey:@"Director"]); // Write the array to file NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/discus.plist"]; [discs writeToFile:path atomically:YES]; // Read the array back from the file, and write it to the console NSArray *newDiscs = [[[NSArray alloc] initWithContentsOfFile:path] autorelease]; NSLog(@"The database contents are:\n%@", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required