Class

NSKeyed​Archiver

NSKeyed​Archiver, a concrete subclass of NSCoder, provides a way to encode objects (and scalar values) into an architecture-independent format that can be stored in a file. When you archive a set of objects, the class information and instance variables for each object are written to the archive. NSKeyed​Archiver’s companion class, NSKeyed​Unarchiver, decodes the data in an archive and creates a set of objects equivalent to the original set.

Overview

A keyed archive differs from a non-keyed archive in that all the objects and values encoded into the archive are given names, or keys. When decoding a non-keyed archive, values have to be decoded in the same order in which they were encoded. When decoding a keyed archive, because values are requested by name, values can be decoded out of sequence or not at all. Keyed archives, therefore, provide better support for forward and backward compatibility.

The keys given to encoded values must be unique only within the scope of the current object being encoded. A keyed archive is hierarchical, so the keys used by object A to encode its instance variables do not conflict with the keys used by object B, even if A and B are instances of the same class. Within a single object, however, the keys used by a subclass can conflict with keys used in its superclasses.

An NSArchiver object can write the archive data to a file or to a mutable-data object (an instance of NSMutable​Data) that you provide.

Symbols

Initializing an NSKeyedArchiver Object

init(for​Writing​With:​ NSMutable​Data)

Returns the receiver, initialized for encoding an archive into a given a mutable-data object.

Archiving Data

class func archived​Data(with​Root​Object:​ Any)

Returns an NSData object containing the encoded form of the object graph whose root object is given.

class func archive​Root​Object(Any, to​File:​ String)

Archives an object graph rooted at a given object by encoding it into a data object then atomically writes the resulting data object to a file at a given path, and returns a Boolean value that indicates whether the operation was successful.

func finish​Encoding()

Instructs the receiver to construct the final data stream.

var requires​Secure​Coding:​ Bool

Indicates whether the receiver requires all archived classes to conform to NSSecure​Coding.

Encoding Data and Objects

func encode(Bool, for​Key:​ String)

Encodes a given Boolean value and associates it with a given key.

func encode​Bytes(Unsafe​Pointer<UInt8>?, length:​ Int, for​Key:​ String)

Encodes a given number of bytes from a given C array of bytes and associates them with the a given key.

func encode​Conditional​Object(Any?, for​Key:​ String)

Encodes a reference to a given object and associates it with a given key only if it has been unconditionally encoded elsewhere in the archive with encode(_:​for​Key:​).

func encode(Double, for​Key:​ String)

Encodes a given double value and associates it with a given key.

func encode(Float, for​Key:​ String)

Encodes a given float value and associates it with a given key.

func encode(Int32, for​Key:​ String)

Encodes a given 32-bit integer value and associates it with a given key.

func encode(Int64, for​Key:​ String)

Encodes a given 64-bit integer value and associates it with a given key.

func encode(Any?, for​Key:​ String)

Encodes a given object and associates it with a given key.

Managing the Delegate

Managing Classes and Class Names

class func set​Class​Name(String?, for:​ Any​Class)

Adds a class translation mapping to NSKeyed​Archiver whereby instances of of a given class are encoded with a given class name instead of their real class names.

class func class​Name(for:​ Any​Class)

Returns the class name with which NSKeyed​Archiver encodes instances of a given class.

func set​Class​Name(String?, for:​ Any​Class)

Adds a class translation mapping to the receiver whereby instances of of a given class are encoded with a given class name instead of their real class names.

func class​Name(for:​ Any​Class)

Returns the class name with which the receiver encodes instances of a given class.

Constants

Keyed Archiving Exception Names

Names of exceptions that are raised by NSKeyed​Archiver if there is a problem creating an archive.

Keyed Archiver Root Object Key

Keys that NSKeyed​Archiver uses in the hierarchy of encoded objects.

Initializers

Instance Properties

var encoded​Data:​ Data

Returns the encoded data for the archiver.

Relationships

Inherits From