# NSArchiver class reference

mccallum@gnu.ai.mit.edu
rfm@gnu.org
Generated at 2025-12-02 21:52:00 +0000
1995, 1996, 1997, 1998 Free Software Foundation, Inc.
Software documentation for the NSArchiver class
Foundation/NSArchiver.h

Implementation of NSCoder capable of creating sequential archives which must be read in the same order they were written. This class implements methods for saving to and restoring from a serial archive (usually a file on disk, but can be an NSData object) as well as methods that can be used by objects that need to write/restore themselves.

Note, the sibling class NSKeyedArchiversupports a form of archive that is more robust to class changes, and is recommended over this one.

archiveRootObject:
rootObject
toFile:
path
Writes out serialized representation of object and, recursively, any other objects it holds references to.
archivedDataWithRootObject:
rootObject
Writes serialized representation of object and, recursively, any other objects it holds references to, to byte array.
archiverData
Returns whatever data has been encoded thus far.
classNameEncodedForTrueClassName:
trueName
Returns substitute class used to encode objects of given class. This would have been set through an earlier call to [NSArchiver -encodeClassName:intoClassName:].
encodeClassName:
trueName
intoClassName:
inArchiveName
Specify substitute class used in archiving objects of given class. This class is written to the archive as the class to use for restoring the object, instead of what is returned from [NSObject -classForArchiver]. This can be used to provide backward compatibility across class name changes. The object is still encoded by calling `encodeWithCoder:` as normal.
initForWritingWithMutableData:
mdata
Init instance that will archive its data to mdata. (Even if [archiveRootObject:toFile:] is called, this still gets written to.)
replaceObject:
object
withObject:
newObject
Set encoder to write out newObject in place of object.
Software documentation for the NSUnarchiver class
Foundation/NSArchiver.h

This class reconstructs objects from an archive.

Re-using the archiver

The -resetUnarchiverWithData:atIndex: method lets you re-use the archive to decode a new data object or, in conjunction with the 'cursor' method (which reports the current decoding position in the archive), decode a second archive that exists in the data object after the first one.

Subclassing with different input format.
NSUnarchiver
normally reads directly from an NSData object using the methods -
-deserializeTypeTag:andCrossRef:atCursor:
to decode type tags for data items, the tag is the first byte of the character encoding string for the data type (as provided by '@encode(xxx)'), possibly with the top bit set to indicate that what follows is a crossreference to an item already encoded.
Also decode a crossreference number either to identify the following item, or to refer to a previously encoded item. Objects, Classes, Selectors, CStrings and Pointer items have crossreference encoding, other types do not.
[NSData -deserializeDataAt:ofObjCType:atCursor:context:]
to decode all other information.

`NSUnarchiver` normally uses other NSData methods to read the archive header information from within the method: [-deserializeHeaderAt:version:classes:objects:pointers:] to read a fixed size header including archiver version (obtained by `[self systemVersion]`) and crossreference table sizes.

To subclass `NSUnarchiver`, you must implement your own versions of the four methods above, and override the 'directDataAccess' method to return `NO` so that the archiver knows to use your serialization methods rather than those in the NSData object.

classNameDecodedForArchiveClassName:
nameInArchive
Returns class name unarchivers will use to instantiate encoded objects when they report their class name as nameInArchive.
decodeClassName:
nameInArchive
asClassName:
trueName
Sets class name unarchivers will use to instantiate encoded objects when they report their class name as nameInArchive. This can be used to support backwards compatibility across class name changes.
unarchiveObjectWithData:
anObject
Creates an NSUnarchiver to read from anObject and returns result of sending [NSCoder -decodeObject] to it.
unarchiveObjectWithFile:
path
Creates an NSUnarchiver to read from path and returns result of sending [NSCoder -decodeObject] to it.
classNameDecodedForArchiveClassName:
nameInArchive
Returns class name this unarchiver uses to instantiate encoded objects when they report their class name as nameInArchive.
decodeClassName:
nameInArchive
asClassName:
trueName
Set class name this unarchiver uses to instantiate encoded objects when they report their class name as nameInArchive. This can be used to provide backward compatibility across class name changes.
initForReadingWithData:
anObject
Set up to read objects from data buffer anObject.
isAtEnd
Returns whether have currently read through all of data buffer or file this unarchiver was initialized with.
objectZone
Returns zone unarchived objects will be allocated from.
replaceObject:
anObject
withObject:
replacement
Set unarchiver to replace anObject with replacement whenever it is found decoded from the archive.
setObjectZone:
aZone
Sets zone unarchived objects will be allocated from.
systemVersion
Returns system version archive was encoded by.
Software documentation for the NSArchiver(GNUstep) category
Foundation/NSArchiver.h
Category for compatibility with old GNUstep encoding.
directDataAccess
Returns `YES`.
resetArchiver
Allow reuse of archiver (clears class substitution maps, etc.) but do not clear out current serialized data.
serializeHeaderAt:
positionInData
version:
systemVersion
classes:
classCount
objects:
objectCount
pointers:
pointerCount
Writes out header for GNUstep archive format.
Software documentation for the NSUnarchiver(GNUstep) category
Foundation/NSArchiver.h
Category for compatibility with old GNUstep encoding.
cursor
Return current position within archive byte array.
deserializeHeaderAt:
pos
version:
v
classes:
c
objects:
o
pointers:
p
Reads in header for GNUstep archive format.
directDataAccess
Returns `YES`.
resetUnarchiverWithData:
anObject
atIndex:
pos
Prepare for reuse of the unarchiver to unpack a new archive, specified in anObject, starting at pos. Reads archive header.