[ Team LiB ] Previous Section Next Section

Archiving with NSArchiver

A more flexible approach allows for the saving of any types of objects to a file, not just strings, arrays, and dictionaries. Let's begin with the simple glossary created in Chapter 15. Program 19.3 shows how the glossary can be saved to a file on disk using the method archiveRootObject:toFile: from the NSArchiver class. To use this class, include the file


#import <Foundation/NSArchiver.h>

in your program.

Program 19.3
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArchiver.h>
#import <Foundation/NSAutoreleasePool.h>

int main (int argc, char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSDictionary *glossary =
   [NSDictionary dictionaryWithObjectsAndKeys:
     @"A class defined so other classes can inherit from it",
     @"abstract class",
     @"To implement all the methods defined in a protocol",
     @"adopt",
     @"Storing an object for later use",
     @"archiving",
     nil
   ];


  [NSArchiver archiveRootObject: glossary toFile: @"glossary.archive"];

  [pool release];
  return 0;
}

Program 19.3 does not produce any output at the terminal. However, the statement


[NSArchiver archiveRootObject: glossary toFile: @"glossary.archive"];

writes the dictionary glossary to the file glossary.archive. Any pathname can be specified for the file. In this case, the file is written to the current directory.

The archive file created can later be read into an executing program by using NSUnarchiver's unArchiveObjectWithFile: method, as is done in Program 19.4.

Program 19.4
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSArchiver.h>
#import <Foundation/NSAutoreleasePool.h>

int main (int argc, char *argv[])
{
  NSAutoreleasePool  *pool = [[NSAutoreleasePool alloc] init];
  NSDictionary *glossary;
  NSEnumerator *keyEnum;
  id       key;

  glossary = [NSUnarchiver unarchiveObjectWithFile:
                  @"glossary.archive"];

  keyEnum = [glossary keyEnumerator];

  while ( (key = [keyEnum nextObject]) != nil ) {
        printf ("%s: %s\n\n", [key cString],
        [[glossary objectForKey: key] cString]);
  }

  [pool release];
  return 0;
}
Program 19.4 Output
abstract class: A class defined so other classes can inherit from it.

adopt: To implement all the methods defined in a protocol

archiving: Storing an object for later use.

The statement


glossary = [NSUnarchiver unarchiveObjectWithFile:
                @"glossary.archive"];

causes the specified file to be opened and its contents to be read. This file must be the result of a previous archive operation. You can specify a full pathname for the file or a relative path name, as was done in the example.

After the glossary has been restored, the program simply enumerates its contents to verify that the restore was successful.

    [ Team LiB ] Previous Section Next Section