<!--
{
  "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/NSKeyedUnarchiver",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSKeyedUnarchiver"
  },
  "title" : "NSKeyedUnarchiver"
}
-->

# NSKeyedUnarchiver

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

```
class NSKeyedUnarchiver
```

## Overview

[`NSKeyedUnarchiver`](/documentation/Foundation/NSKeyedUnarchiver) is a concrete subclass of [`NSCoder`](/documentation/Foundation/NSCoder) that defines methods for decoding a set of named objects (and scalar values) from a keyed archive. The [`NSKeyedArchiver`](/documentation/Foundation/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, [`decodeBool(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeBool(forKey:)) returns <doc://com.apple.documentation/documentation/Swift/false>, [`decodeIntForKey:`](/documentation/Foundation/NSKeyedUnarchiver/decodeIntForKey:) returns `0`, and [`decodeObject(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeObject(forKey:)) returns `nil`.

[`NSKeyedUnarchiver`](/documentation/Foundation/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 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 [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException). Further, when trying to coerce a value to an incompatible type — for example decoding an `Int` as a `Float` — the decoding method throws an [`invalidUnarchiveOperationException`](/documentation/Foundation/NSExceptionName/invalidUnarchiveOperationException).

## Topics

### Creating a Keyed Unarchiver

[`init(forReadingFrom:)`](/documentation/Foundation/NSKeyedUnarchiver/init(forReadingFrom:))

Initializes an archiver to decode data from the specified location.

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

Initializes an archiver to decode data.

[`init(forReadingWith:)`](/documentation/Foundation/NSKeyedUnarchiver/init(forReadingWith:))

Initializes an archiver to decode data from the specified location.

### Unarchiving Data

[`unarchiveTopLevelObjectWithData(_:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchiveTopLevelObjectWithData(_:))

Decodes a previously-archived object graph, and returns the root object.

[`unarchivedObject(ofClass:from:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchivedObject(ofClass:from:))

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

[`unarchivedObjectOfClass:fromData:error:`](/documentation/Foundation/NSKeyedUnarchiver/unarchivedObjectOfClass:fromData:error:)

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

[`unarchivedObject(ofClasses:from:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchivedObject(ofClasses:from:)-b9t5)

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

[`unarchivedObject(ofClasses:from:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchivedObject(ofClasses:from:)-3h32t)

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

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

Indicates whether the receiver requires all unarchived classes to conform to [`NSSecureCoding`](/documentation/Foundation/NSSecureCoding).

[`unarchiveObject(with:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchiveObject(with:))

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

[`unarchiveTopLevelObjectWithData:error:`](/documentation/Foundation/NSKeyedUnarchiver/unarchiveTopLevelObjectWithData:error:)

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

[`unarchiveObject(withFile:)`](/documentation/Foundation/NSKeyedUnarchiver/unarchiveObject(withFile:))

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

### Decoding Data

[`containsValue(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/containsValue(forKey:))

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

[`decodeDecodable(_:forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeDecodable(_:forKey:))

Decodes a decodable value associated with a given key.

[`decodeTopLevelDecodable(_:forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeTopLevelDecodable(_:forKey:))

Decodes a top-level decodable value associated with a given key.

[`decodeBool(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeBool(forKey:))

Decodes a Boolean value associated with a given key.

[`decodeBytes(forKey:returnedLength:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeBytes(forKey:returnedLength:))

Decodes a stream of bytes associated with a given key.

[`decodeDouble(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeDouble(forKey:))

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

[`decodeFloat(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeFloat(forKey:))

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

[`decodeIntForKey:`](/documentation/Foundation/NSKeyedUnarchiver/decodeIntForKey:)

Decodes an integer value associated with a given key.

[`decodeInt32(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeInt32(forKey:))

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

[`decodeInt64(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeInt64(forKey:))

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

[`decodeObject(forKey:)`](/documentation/Foundation/NSKeyedUnarchiver/decodeObject(forKey:))

Decodes and returns an object associated with a given key.

[`finishDecoding()`](/documentation/Foundation/NSKeyedUnarchiver/finishDecoding())

Tells the receiver that you are finished decoding objects.

[`decodingFailurePolicy`](/documentation/Foundation/NSKeyedUnarchiver/decodingFailurePolicy)

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

### Managing the Delegate

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

The receiver’s delegate.

### Managing Class Names

[`setClass(_:forClassName:)`](/documentation/Foundation/NSKeyedUnarchiver/setClass(_:forClassName:)-swift.type.method)

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

[`class(forClassName:)`](/documentation/Foundation/NSKeyedUnarchiver/class(forClassName:)-swift.type.method)

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

[`setClass(_:forClassName:)`](/documentation/Foundation/NSKeyedUnarchiver/setClass(_:forClassName:)-swift.method)

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

[`class(forClassName:)`](/documentation/Foundation/NSKeyedUnarchiver/class(forClassName:)-swift.method)

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

### Constants

[Keyed Unarchiving Exception Names](/documentation/Foundation/keyed-unarchiving-exception-names)

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



---

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)