<!--
{
  "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/NSData/subdata(with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSData(im)subdataWithRange:"
  },
  "title" : "subdata(with:)"
}
-->

# subdata(with:)

Returns a new data object containing the data object’s bytes that fall within the limits specified by a given range.

```
func subdata(with range: NSRange) -> Data
```

## Parameters

`range`

The range in the receiver from which to get the data. If this range is not within the data object’s range of bytes, [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException) is raised.

## Return Value

A data object containing the receiver’s bytes that fall within the limits specified by `range`.

## Discussion

A sample using this method can be found in [Working With Binary Data](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/BinaryData/Tasks/WorkingBinaryData.html#//apple_ref/doc/uid/20000717).

---

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)