---
title: "decodeTopLevelObjectOfClass:forKey:error:"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscoder/decodetoplevelobjectofclass:forkey:error:"
---

# decodeTopLevelObjectOfClass:forKey:error:

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

## Declaration

```occ
- (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 doc://com.apple.foundation/documentation/Foundation/NSError indicating why decoding failed, or nil if no error occurred.

## Return Value

Return Value The decoded object, or nil if decoding fails.

## Discussion

Discussion If the coder responds true to requiresSecureCoding, then the coder calls failWithError(_:) in either the following cases: The class indicated by cls does not implement 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.

## See Also

### Decoding Top-Level Objects

- [decodeTopLevelObjectOfClasses:forKey:error:](foundation/nscoder/decodetoplevelobjectofclasses:forkey:error:.md)
- [decodeTopLevelObjectAndReturnError:](foundation/nscoder/decodetoplevelobjectandreturnerror:.md)
- [decodeTopLevelObjectForKey:error:](foundation/nscoder/decodetoplevelobjectforkey:error:.md)
