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

# decodeXPCObject(ofType:forKey:)

Decodes an object and validates that its type matches the type a service provides over XPC.

```
func decodeXPCObject(ofType type: xpc_type_t, forKey key: String) -> xpc_object_t?
```

## Parameters

`type`

An opaque pointer to an encoded XPC object.

`key`

A string that your app uses to reference the decoded object.

## Return Value

An object that XPC can encode.

## Discussion

The [`decodeXPCObject(ofType:forKey:)`](/documentation/Foundation/NSXPCCoder/decodeXPCObject(ofType:forKey:)) method validates that the type of the decoded object matches the type of the encoded object. If they don’t match, the [`NSXPCCoder`](/documentation/Foundation/NSXPCCoder) throws an exception in support of [`NSSecureCoding`](/documentation/Foundation/NSSecureCoding).

Be sure to check the result against [`null`](/documentation/Foundation/NSNull/null) if you call an <doc://com.apple.documentation/documentation/XPC> function because calling an <doc://com.apple.documentation/documentation/XPC> function on a [`null`](/documentation/Foundation/NSNull/null) object results in a crash.

---

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)