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

# decodeData()

Decodes and returns an `NSData` object that was previously encoded with [`encode(_:)`](/documentation/Foundation/NSCoder/encode(_:)-1qd1e). Subclasses must override this method.

```
func decodeData() -> Data?
```

## Discussion

The implementation of your overriding method must match the implementation of your [`encode(_:)`](/documentation/Foundation/NSCoder/encode(_:)-1qd1e) method. For example, a typical [`encode(_:)`](/documentation/Foundation/NSCoder/encode(_:)-1qd1e) method encodes the number of bytes of data followed by the bytes themselves. Your override of this method must read the number of bytes, create an `NSData` object of the appropriate size, and decode the bytes into the new `NSData` 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)