Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSArchiver.h |
Related sample code |
NSUnarchiver
, a concrete subclass of NSCoder
, defines methods for decoding a set of Objective-C objects from an archive. Such archives are produced by objects of the NSArchiver
class.
In Mac OS X v10.2 and later, NSArchiver
and NSUnarchiver
have been replaced by NSKeyedArchiver
and NSKeyedUnarchiver
respectively—see Archives and Serializations Programming Guide for Cocoa.
+ classNameDecodedForArchiveClassName:
+ decodeClassName:asClassName:
– classNameDecodedForArchiveClassName:
– decodeClassName:asClassName:
– replaceObject:withObject:
Returns the name of the class used when instantiating objects whose ostensible class, according to the archived data, is a given name.
+ (NSString *)classNameDecodedForArchiveClassName:(NSString *)nameInArchive
The name of a class.
The name of the class used when instantiating objects whose ostensible class, according to the archived data, is nameInArchive. Returns nameInArchive if no substitute name has been specified using the class method (not the instance method) decodeClassName:asClassName:
.
Note that each individual instance of NSUnarchiver
can be given its own class name mappings by invoking the instance method decodeClassName:asClassName:
. The NSUnarchiver
class has no information about these instance-specific mappings, however, so they don’t affect the return value of classNameDecodedForArchiveClassName:
.
NSArchiver.h
Instructs instances of NSUnarchiver
to use the class with a given name when instantiating objects whose ostensible class, according to the archived data, is another given name.
+ (void)decodeClassName:(NSString *)nameInArchive asClassName:(NSString *)trueName
The ostensible name of a class in an archive.
The name of the class to use when instantiating objects whose ostensible class, according to the archived data, is nameInArchive.
This method enables easy conversion of unarchived data when the name of a class has changed since the archive was created.
Note that there is also an instance method of the same name. An instance of NSUnarchiver
can maintain its own mapping of class names. However, if both the class method and the instance method have been invoked using an identical value for nameInArchive, the class method takes precedence.
NSArchiver.h
Decodes and returns the object archived in a given NSData
object.
+ (id)unarchiveObjectWithData:(NSData *)data
An NSData
object that contains an archive created using NSArchiver
.
The object, or object graph, that was archived in data. Returns nil
if data cannot be unarchived.
This method invokes initForReadingWithData:
and decodeObject
to create a temporary NSUnarchiver
object that decodes the object. If the archived object is the root of a graph of objects, the entire graph is unarchived.
encodeRootObject:
(NSArchiver
)NSArchiver.h
Decodes and returns the object archived in the file path.
+ (id)unarchiveObjectWithFile:(NSString *)path
The path to a file than contains an archive created using NSArchiver
.
The object, or object graph, that was archived in the file at path. Returns nil
if the file at path cannot be unarchived.
This convenience method reads the file by invoking the NSData
method dataWithContentsOfFile:
and then invokes unarchiveObjectWithData:
.
NSArchiver.h
Returns the name of the class that will be used when instantiating objects whose ostensible class, according to the archived data, is a given name.
- (NSString *)classNameDecodedForArchiveClassName:(NSString *)nameInArchive
The ostensible name of a class in an archive.
The name of the class that will be used when instantiating objects whose ostensible class, according to the archived data, is nameInArchive. Returns nameInArchive unless a substitute name has been specified using the instance method (not the class method) decodeClassName:asClassName:
.
NSArchiver.h
Instructs the receiver to use the class with a given name when instantiating objects whose ostensible class, according to the archived data, is another given name.
- (void)decodeClassName:(NSString *)nameInArchive asClassName:(NSString *)trueName
The ostensible name of a class in an archive.
The name of the class to use when instantiating objects whose ostensible class, according to the archived data, is nameInArchive.
This method enables easy conversion of unarchived data when the name of a class has changed since the archive was created.
Note that there’s also a class method of the same name. The class method has precedence in case of conflicts.
NSArchiver.h
Returns an NSUnarchiver
object initialized to read an archive from a given data object.
- (id)initForReadingWithData:(NSData *)data
The archive data.
An NSUnarchiver
object initialized to read an archive from data. Returns nil
if data is not a valid archive.
The method decodes the system version number that was archived in data prepares the NSUnarchiver
object for a subsequent invocation of decodeObject
.
Raises an NSInvalidArgumentException
if data is nil
.
NSArchiver.h
Returns a Boolean value that indicates whether the receiver has reached the end of the encoded data while decoding.
- (BOOL)isAtEnd
YES
if the receiver has reached the end of the encoded data while decoding, otherwise NO
.
You can invoke this method after invoking decodeObject
to discover whether the archive contains extra data following the encoded object graph. If it does, you can either ignore this anomaly or consider it an error.
NSArchiver.h
Returns the memory zone used to allocate decoded objects.
- (NSZone *)objectZone
The memory zone used to allocate decoded objects.
NSArchiver.h
Causes the receiver to substitute one given object for another whenever the latter is extracted from the archive.
- (void)replaceObject:(id)object withObject:(id)newObject
The archived object to replace.
The object with which to replace object.
newObject can be of a different class from object, and the class mappings set by classNameDecodedForArchiveClassName:
and decodeClassName:asClassName:
are ignored.
NSArchiver.h
Sets the memory zone used to allocate decoded objects.
- (void)setObjectZone:(NSZone *)zone
The memory zone used to allocate decoded objects.
If zone is nil
, or if this method is never invoked, the default zone is used, as given by NSDefaultMallocZone()
.
NSArchiver.h
Returns the system version number in effect when the archive was created.
- (unsigned)systemVersion
The system version number in effect when the archive was created.
This information is available as soon as the receiver has been initialized.
NSArchiver.h
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-05-23)