Class

NSKeyedUnarchiver

A decoder that restores data from an archive referenced by keys.

Declaration

@interface NSKeyedUnarchiver : NSCoder

Overview

NSKeyedUnarchiver is a concrete subclass of NSCoder that defines methods for decoding a set of named objects (and scalar values) from a keyed archive. The NSKeyedArchiver class produces archives that this class can decode.

The archiver creates keyed archive as a hierarchy of objects. The archiver treats each object as a namespace into which it can encode other objects. This means that an unarchiver can only decode objects encoded within the immediate scope of their parent object. Objects encoded elsewhere in the hierarchy — whether higher than, lower than, or parallel to this particular object — aren’t accessible. In this way, the keys used by a particular object to encode its instance variables need to be unique only within the scope of that object.

If you invoke one of the decode-prefixed methods of this class using a key that does not exist in the archive, the return value indicates failure. This value varies by decoded type. For example, if a key does not exist in an archive, decodeBoolForKey: returns NO, decodeIntForKey: returns 0, and decodeObjectForKey: returns nil.

NSKeyedUnarchiver supports limited type coercion for numeric types. You can use any of the integer decode methods to decode a value encoded as any type of integer, whether a standard int or an explicit 32-bit or 64-bit integer. Likewise, you can use the float- or double-returning decode methods to handle a value encoded as a float or double. If an encoded value is too large to fit within the coerced type, the decoding method throws a NSRangeException. Further, when trying to coerce a value to an incompatible type — for example decoding an int as a float — the decoding method throws an NSInvalidUnarchiveOperationException.

Topics

Creating a Keyed Unarchiver

- initForReadingFromData:error:

Initializes an archiver to decode data from the specified location.

- init

Initializes an archiver to decode data.

Deprecated
- initForReadingWithData:

Initializes an archiver to decode data from the specified location.

Deprecated

Unarchiving Data

+ unarchivedObjectOfClass:fromData:error:

Decodes a previously-archived object graph, that returns the root object as the specified type.

+ unarchivedObjectOfClasses:fromData:error:

Decodes a previously-archived object graph, returning the root object as one of the specified classes.

requiresSecureCoding

Indicates whether the receiver requires all unarchived classes to conform to NSSecureCoding.

+ unarchiveObjectWithData:

Decodes and returns the object graph previously encoded by NSKeyedArchiver and stored in a given NSData object.

Deprecated
+ unarchiveTopLevelObjectWithData:error:

Decodes a previously-archived object graph, returning the root object.

Deprecated
+ unarchiveObjectWithFile:

Decodes and returns the object graph previously encoded by NSKeyedArchiver written to the file at a given path.

Deprecated

Decoding Data

- containsValueForKey:

Returns a Boolean value that indicates whether the archive contains a value for a given key within the current decoding scope.

- decodeBoolForKey:

Decodes a Boolean value associated with a given key.

- decodeBytesForKey:returnedLength:

Decodes a stream of bytes associated with a given key.

- decodeDoubleForKey:

Decodes a double-precision floating-point value associated with a given key.

- decodeFloatForKey:

Decodes a single-precision floating-point value associated with a given key.

- decodeIntForKey:

Decodes an integer value associated with a given key.

- decodeInt32ForKey:

Decodes a 32-bit integer value associated with a given key.

- decodeInt64ForKey:

Decodes a 64-bit integer value associated with a given key.

- decodeObjectForKey:

Decodes and returns an object associated with a given key.

- finishDecoding

Tells the receiver that you are finished decoding objects.

decodingFailurePolicy

The action to take when this unarchiver fails to decode an entry.

Managing the Delegate

delegate

The receiver’s delegate.

Managing Class Names

+ setClass:forClassName:

Sets a global translation mapping to decode objects encoded with a given class name as instances of a given class instead.

+ classForClassName:

Returns the class from which this unarchiver instantiates an encoded object with a given class name.

- setClass:forClassName:

Sets a translation mapping on this unarchiver to decode objects encoded with a given class name as instances of a given class instead.

- classForClassName:

Returns the class from which this unarchiver instantiates an encoded object with a given class name.

Constants

Keyed Unarchiving Exception Names

Names of exceptions that are raised by NSKeyedUnarchiver if there is a problem extracting an archive.

Relationships

Inherits From

See Also

Keyed Archivers

NSKeyedArchiver

An encoder that stores an object’s data to an archive referenced by keys.

NSKeyedArchiverDelegate

The optional methods implemented by the delegate of a keyed archiver.

NSKeyedUnarchiverDelegate

The optional methods implemented by the delegate of a keyed unarchiver.

NSCoder

An abstract class that serves as the basis for objects that enable archiving and distribution of other objects.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software