Class

NSKeyedUnarchiver

NSKeyedUnarchiver, a concrete subclass of NSCoder, defines methods for decoding a set of named objects (and scalar values) from a keyed archive. Such archives are produced by instances of the NSKeyedArchiver class.

Overview

A keyed archive is encoded as a hierarchy of objects. Each object in the hierarchy serves as a namespace into which other objects are encoded. The objects available for decoding are restricted to those that were encoded within the immediate scope of a particular object. Objects encoded elsewhere in the hierarchy, whether higher than, lower than, or parallel to this particular object, are not 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... methods of this class using a key that does not exist in the archive, a non-positive value is returned. This value varies by decoded type. For example, if a key does not exist in an archive, decodeBool(forKey:) returns false, decodeInt(forKey:) returns 0, and decodeObject(forKey:) returns nil.

NSKeyedUnarchiver supports limited type coercion. A value encoded as any type of integer, whether a standard int or an explicit 32-bit or 64-bit integer, can be decoded using any of the integer decode methods. Likewise, a value encoded as a float or double can be decoded as either a float or a double value. If an encoded value is too large to fit within the coerced type, the decoding method throws an 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.

Symbols

Initializing a Keyed Unarchiver

init(forReadingWith: Data)

Initializes the receiver for decoding an archive previously encoded by NSKeyedArchiver.

Unarchiving Data

class func unarchiveObject(with: Data)

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

class func unarchiveObject(withFile: String)

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

var requiresSecureCoding: Bool

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

Decoding Data

func containsValue(forKey: String)

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

func decodeBool(forKey: String)

Decodes a Boolean value associated with a given key.

func decodeBytes(forKey: String, returnedLength: UnsafeMutablePointer<Int>?)

Decodes a stream of bytes associated with a given key.

func decodeDouble(forKey: String)

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

func decodeFloat(forKey: String)

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

func decodeInt32(forKey: String)

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

func decodeInt64(forKey: String)

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

func decodeObject(forKey: String)

Decodes and returns an object associated with a given key.

func finishDecoding()

Tells the receiver that you are finished decoding objects.

Managing the Delegate

Managing Class Names

class func setClass(AnyClass?, forClassName: String)

Adds a class translation mapping to NSKeyedUnarchiver whereby objects encoded with a given class name are decoded as instances of a given class instead.

class func `class`(forClassName: String)

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

func setClass(AnyClass?, forClassName: String)

Adds a class translation mapping to the receiver whereby objects encoded with a given class name are decoded as instances of a given class instead.

func `class`(forClassName: String)

Returns the class from which the receiver 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

Conforms To