---
title: "decodeObject(of:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscoder/decodeobject(of:forkey:)-7tmft"
---

# decodeObject(of:forKey:)

Decode an object as an expected type, failing if the archived type doesn’t match.

## Declaration

```swift
func decodeObject<DecodedObjectType>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType? where DecodedObjectType : NSObject, DecodedObjectType : NSCoding
```

## Parameters

- `cls`: The expected class of the object being decoded.
- `key`: The key indicating the member to decode.

## 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 of the following cases: The class indicated by cls doesn’t implement NSSecureCoding. The unarchived class doesn’t match cls, nor do any of its superclasses. If the coder doesn’t require secure coding, it ignores the cls parameter and does not check the decoded object.

## See Also

### Decoding Top-Level Objects

- [decodeObject(of:forKey:)](foundation/nscoder/decodeobject(of:forkey:)-roif.md)
- [decodeTopLevelObject()](foundation/nscoder/decodetoplevelobject().md)
- [decodeTopLevelObject(forKey:)](foundation/nscoder/decodetoplevelobject(forkey:).md)
- [decodeTopLevelObject(of:forKey:)](foundation/nscoder/decodetoplevelobject(of:forkey:)-3w6pd.md)
- [decodeTopLevelObject(of:forKey:)](foundation/nscoder/decodetoplevelobject(of:forkey:)-5lnnn.md)
