<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 11.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 4.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSKeyedUnarchiver/unarchivedObject(ofClass:from:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So17NSKeyedUnarchiverC10FoundationE16unarchivedObject7ofClass4fromxSgxm_AC4DataVtKSo8NSObjectCRbzSo8NSCodingRzlFZ"
  },
  "title" : "unarchivedObject(ofClass:from:)"
}
-->

# unarchivedObject(ofClass:from:)

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

```
@nonobjc static func unarchivedObject<DecodedObjectType>(ofClass cls: DecodedObjectType.Type, from data: Data) throws -> DecodedObjectType? where DecodedObjectType : NSObject, DecodedObjectType : NSCoding
```

## Parameters

`cls`

The expected class of the root object.

`data`

An object graph previously encoded by [`NSKeyedArchiver`](/documentation/Foundation/NSKeyedArchiver).

## Return Value

The decoded root of the object graph, or `nil` if an error occurred.

## Discussion

This method produces an error if `data` does not contain valid keyed data.

> Important:
> Make sure you have adopted ``doc://com.apple.foundation/documentation/Foundation/NSSecureCoding`` in the types you decode. If any call to a `decode`-prefixed method fails, the default ``doc://com.apple.foundation/documentation/Foundation/NSKeyedUnarchiver/decodingFailurePolicy`` sets the error rather than throwing an exception. In this case, the current and all subsequent decode calls return `0` or `nil`.

---

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)