<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCoder/decodeTopLevelObjectOfClass:forKey:error:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCoder(im)decodeTopLevelObjectOfClass:forKey:error:"
  },
  "title" : "decodeTopLevelObjectOfClass:forKey:error:"
}
-->

# decodeTopLevelObjectOfClass:forKey:error:

Decode an object as an expected type, failing if the archived type does not match.

```
- (id) decodeTopLevelObjectOfClass:(Class) aClass forKey:(NSString *) key error:(NSError **) error;
```

## Parameters

`aClass`

The expected class of the object being decoded.

`key`

The archive key indicating the member to decode.

`error`

On return, an [`NSError`](/documentation/Foundation/NSError) indicating why decoding failed, or `nil` if no error occurred.

## Return Value

The decoded object, or `nil` if decoding fails.

## Discussion

If the coder responds <doc://com.apple.documentation/documentation/Swift/true> to [`requiresSecureCoding`](/documentation/Foundation/NSCoder/requiresSecureCoding), then the coder calls [`failWithError(_:)`](/documentation/Foundation/NSCoder/failWithError(_:)) in either the following cases:

- The class indicated by `cls` does not implement [`NSSecureCoding`](/documentation/Foundation/NSSecureCoding).
- The unarchived class does not match `cls`, nor do any of its superclasses.

If the coder does not require secure coding, it ignores the `cls` parameter and does not check the decoded object.

---

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)