Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSArchiver

Inherits From: NSCoder : NSObject

Conforms To: NSObject (NSObject)

Declared In: Foundation/NSArchiver.h

Class Description

NSArchiver, a concrete subclass of NSCoder, defines an object that encodes Objective C objects into an architecture-independent format that can be stored in a file. When objects are archived, their class information and the values of their instance variables are written to the archive. NSArchiver's companion class, NSUnarchiver, takes an archive file and decodes its contents into a set of objects equivalent to the original one.

Archiving is typically initiated by sending an NSArchiver an encodeRootObject: or archiveRootObject:toFile: message. These messages specify a single object that is the starting point for archiving. The root object receives an encodeWithCoder: message (see the NSCoding protocol) that allows it to begin archiving itself and the other objects that it's connected to. An object responds to an encodeWithCoder: message by writing its instance variables to the archiver.

An object doesn't have to archive the values of each of its instance variables. Some values may not be important to reestablish and others may be derivable from related state upon unarchiving. Other instance variables should be written to the archive only under certain conditions, as explained below.

NSArchiver overrides the inherited encodeRootObject: and encodeConditionalObject: methods to support the conditional archiving of members of a graph of objects. When an object receives an encodeWithCoder: message, it should respond by unconditionally archiving instance variables that are intrinsic to its nature (with the exceptions noted above) and conditionally archiving those that are not. For example, an NSView unconditionally archives its array of subviews (using encodeObject:, or the like) but conditionally archives its superview (using encodeConditionalObject:). The archiving system notes each reference to a conditional object, but doesn't actually archive the object unless some other object in the graph requests the object to be archived unconditionally. This ensures that an object is only archived once despite multiple references to it in the object graph. When the objects are extracted from the archive, the multiple references to objects are resolved, and an equivalent graph of objects is reestablished.

Initializing an NSArchiver

Archiving Data

Getting Data from the NSArchiver

Substituting One Class for Another