---
title: "subdata(with:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdata/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.

## Declaration

```swift
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, doc://com.apple.foundation/documentation/Foundation/NSExceptionName/rangeException is raised.

## Return Value

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

## Discussion

Discussion A sample using this method can be found in Working With Binary Data.

## See Also

### Finding Data

- [range(of:options:in:)](foundation/nsdata/range(of:options:in:).md)
- [NSData.SearchOptions](foundation/nsdata/searchoptions.md)
