NSArchiver

NSArchiver Mac OS X 10.0

This class is a concrete subclass of NSCoder used to archive a network of interconnected objects (an object tree) into a data format that can be written to disk. The archive operation returns an NSMutableData object containing this data. To archive an object, the class method archivedDataWithRootObject: is used, which returns an NSData object containing the archived object. Alternatively, we can archive directly to a file using the class method archiveRootObject:toFile:. It is also possible to initialize an instance of NSArchiver with a pointer to an instance of NSMutableData using initForWritingWithMutableData:, thus providing a more persistent archiving engine than if we simply used the class methods.

To retrieve objects from an archive, we use another subclass of NSCoder: NSUnarchiver. NSArchiver and NSUnarchiver support an archival scheme where objects and variables must be unarchived in the same order that they were archived. Mac OS X 10.2 introduced keyed-archiving, whereby every object and variable in an archive has an associated key that frees us from having to be strictly bound to the original archive format. This has great benefits for improving the compatability of data files between versions of an application.

For instances of a class to be archivable, that class must conform to the NSCoding protocol. See the NSCoding protocol description and Chapter 2 for more information about archiving.

figs/cocn_1306.gif

@interface NSArchiver : NSCoder
 // Initializers
   - (id)initForWritingWithMutableData:(NSMutableData *)mdata;
 // Class Methods
   + (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
   + (NSData *)archivedDataWithRootObject:(id)rootObject;
 // Instance Methods
   - (NSMutableData *)archiverData;
   - (NSString *)classNameEncodedForTrueClassName:(NSString *)trueName;
   - (void)encodeClassName:(NSString *)trueName  intoClassName:(NSString *)inArchiveName;
   - (void)encodeConditionalObject:(id)object;
   - (void)encodeRootObject:(id)rootObject;
   - (void)replaceObject:(id)object withObject:(id)newObject;



    Part II: API Quick Reference
    Chapter 13. Foundation Classes