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%@", ...
Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.