---
title: "unarchivedObject(ofClass:from:)"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/nskeyedunarchiver/unarchivedobject(ofclass:from:)"
---

# unarchivedObject(ofClass:from:)

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

## Declaration

```swift
@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 doc://com.apple.foundation/documentation/Foundation/NSKeyedArchiver.

## Return Value

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

## Discussion

Discussion This method produces an error if data does not contain valid keyed data. important: Make sure you have adopted NSSecureCoding in the types you decode. If any call to a decode-prefixed method fails, the default decodingFailurePolicy sets the error rather than throwing an exception. In this case, the current and all subsequent decode calls return 0 or nil.

## See Also

### Unarchiving Data

- [unarchiveTopLevelObjectWithData(_:)](foundation/nskeyedunarchiver/unarchivetoplevelobjectwithdata(_:).md)
- [unarchivedObject(ofClasses:from:)](foundation/nskeyedunarchiver/unarchivedobject(ofclasses:from:)-b9t5.md)
- [unarchivedObject(ofClasses:from:)](foundation/nskeyedunarchiver/unarchivedobject(ofclasses:from:)-3h32t.md)
- [requiresSecureCoding](foundation/nskeyedunarchiver/requiressecurecoding.md)
- [unarchiveObject(with:)](foundation/nskeyedunarchiver/unarchiveobject(with:).md)
- [unarchiveObject(withFile:)](foundation/nskeyedunarchiver/unarchiveobject(withfile:).md)
