Archiving Objects

Archiving an object (or a collection of interconnected objects, known as a graph) into a NSData representation is often useful or necessary. Objects that are archived into an NSData object can be transported over network connections or interprocess communication channels and saved to the filesystem. Later, the original graph of objects can be reconstituted from the archive data.

Foundation provides five classes to support the creation and extraction of archives, all subclasses of NSCoder:

NSCoder declares the common interface for encoding and decoding objects and other Objective-C data types. For example, the encodeObject method encodes an object into an archive, and methods such as encodeInt: and encodeRect: support encoding C data types such as integers and common Cocoa data structures.

NSCoder does not implement these methods; it is an abstract class. Rather, subclasses implement the appropriate methods for their particular purpose. NSArchiver and NSUnarchiver provide a straightforward way of encoding and decoding objects and scalars, but they have limitations. The biggest limitation is that objects in an archive can be decoded only in the same order in which they were encoded. Because of this constraint, changing an encoding system is difficult once it has been established publicly.

The NSKeyedArchiver and NSKeyedUnarchiver classes solve this problem by associating keys with each object and scalar encoded in an archive. Decoders can use these keys to access an archive’s contents in a convenient order that isn’t constrained by a design decision made in a previous version of the application.

The last NSCoder subclass, NSPortCoder , encodes and decodes object proxies in the distributed objects system. NSConnection uses this class, and as such, you should never have to interact with it. Chapter 6 discusses the distributed objects system in more detail.

Consider Examples Example 2-31 and Example 2-32. Example 2-31 shows the interface for a hypothetical Employee class. Note in this example that the interface declaration indicates that Employee conforms to the NSCoding protocol.

Example 2-32 shows a way to implement the NSCoding protocol for the Employee class.