<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSKeyedArchiver",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSKeyedArchiver"
  },
  "title" : "NSKeyedArchiver"
}
-->

# NSKeyedArchiver

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

```
class NSKeyedArchiver
```

## Overview

[`NSKeyedArchiver`](/documentation/Foundation/NSKeyedArchiver), a concrete subclass of [`NSCoder`](/documentation/Foundation/NSCoder), provides a way to encode objects (and scalar values) into an architecture-independent format suitable for storage in a file. When you archive a set of objects, the archiver writes the class information and instance variables for each object to the archive. The companion class [`NSKeyedUnarchiver`](/documentation/Foundation/NSKeyedUnarchiver) decodes the data in an archive and creates a set of objects equivalent to the original set.

A keyed archive differs from a non-keyed archive in that all the objects and values encoded into the archive have names, or keys. When decoding a non-keyed archive, the decoder must decode values in the same order the original encoder used. When decoding a keyed archive, the decoder requests values by name, meaning it can decode values 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 currently-encoding object. A keyed archive is hierarchical, so the keys used by object A to encode its instance variables don’t conflict with the keys used by object B. This is true 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`](/documentation/Foundation/NSArchiver) object can write the archive data to a file or to a mutable-data object (an instance of [`NSMutableData`](/documentation/Foundation/NSMutableData)) that you provide.

## Topics

### Creating a Keyed Archiver

[`init(requiringSecureCoding:)`](/documentation/Foundation/NSKeyedArchiver/init(requiringSecureCoding:))

Creates an archiver to encode data, and optionally disables secure coding.

[`init()`](/documentation/Foundation/NSKeyedArchiver/init())

Initializes an archiver to encode data.

[`init(forWritingWith:)`](/documentation/Foundation/NSKeyedArchiver/init(forWritingWith:))

Initializes an archiver to encode data into a given a mutable-data object.

### Archiving Data

[`archivedData(withRootObject:requiringSecureCoding:)`](/documentation/Foundation/NSKeyedArchiver/archivedData(withRootObject:requiringSecureCoding:))

Encodes an object graph with the given root object into a data representation, optionally requiring secure coding.

[`finishEncoding()`](/documentation/Foundation/NSKeyedArchiver/finishEncoding())

Instructs the receiver to construct the final data stream.

[`encodedData`](/documentation/Foundation/NSKeyedArchiver/encodedData)

The encoded data for the archiver.

[`outputFormat`](/documentation/Foundation/NSKeyedArchiver/outputFormat)

The format in which the receiver encodes its data.

[`requiresSecureCoding`](/documentation/Foundation/NSKeyedArchiver/requiresSecureCoding)

Indicates whether the archiver requires all archived classes to resist object substitution attacks.

[`archivedData(withRootObject:)`](/documentation/Foundation/NSKeyedArchiver/archivedData(withRootObject:))

Returns a data object that contains the encoded form of the object graph formed by the given root object.

[`archiveRootObject(_:toFile:)`](/documentation/Foundation/NSKeyedArchiver/archiveRootObject(_:toFile:))

Archives an object graph rooted at a given object to a file at a given path.

### Encoding Data and Objects

[`encodeEncodable(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encodeEncodable(_:forKey:))

Encodes a given value and associates it with a key.

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-9pxhm)

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

[`encodeBytes(_:length:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encodeBytes(_:length:forKey:))

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

[`encodeConditionalObject(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encodeConditionalObject(_:forKey:))

Encodes a reference to a given object and associates it with a key only if it has been unconditionally encoded elsewhere in the archive.

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-1mkfl)

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

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-67rcs)

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

[`encodeInt:forKey:`](/documentation/Foundation/NSKeyedArchiver/encodeInt:forKey:)

Encodes a given `int` value and associates it with a key.

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-5i7tc)

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

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-ycdd)

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

[`encode(_:forKey:)`](/documentation/Foundation/NSKeyedArchiver/encode(_:forKey:)-9f4n9)

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

### Managing the Delegate

[`delegate`](/documentation/Foundation/NSKeyedArchiver/delegate)

The archiver’s delegate.

### Managing Classes and Class Names

[`setClassName(_:for:)`](/documentation/Foundation/NSKeyedArchiver/setClassName(_:for:)-swift.type.method)

Sets a global translation mapping to encode instances of a given class with the provided name, rather than their real name.

[`className(for:)`](/documentation/Foundation/NSKeyedArchiver/className(for:)-swift.type.method)

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

[`setClassName(_:for:)`](/documentation/Foundation/NSKeyedArchiver/setClassName(_:for:)-swift.method)

Sets a mapping for this archiver to encode instances of a given class with the provided name, rather than their real name.

[`className(for:)`](/documentation/Foundation/NSKeyedArchiver/className(for:)-swift.method)

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

### Constants

[Keyed Archiving Exception Names](/documentation/Foundation/keyed-archiving-exception-names)

Names of exceptions raised by this class if problems occur while creating an archive.

[Keyed Archiver Root Object Key](/documentation/Foundation/keyed-archiver-root-object-key)

Keys that the archiver uses in the hierarchy of encoded objects.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)