---
title: "range(of:options:in:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdata/range(of:options:in:)"
---

# range(of:options:in:)

Finds and returns the range of the first occurrence of the given data, within the given range, subject to given options.

## Declaration

```swift
func range(of dataToFind: Data, options mask: NSData.SearchOptions = [], in searchRange: NSRange) -> NSRange
```

## Parameters

- `dataToFind`: The data for which to search.
- `mask`: A mask specifying search options. The doc://com.apple.foundation/documentation/Foundation/NSData/SearchOptions options may be specified singly or by combining them with the C bitwise OR operator.
- `searchRange`: The range within the receiver in which to search for dataToFind. 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 An NSRange structure giving the location and length of dataToFind within searchRange, modulo the options in mask. The range returned is relative to the start of the searched data, not the passed-in search range. Returns {``NSNotFound``, 0} if dataToFind is not found or is empty.

## See Also

### Finding Data

- [subdata(with:)](foundation/nsdata/subdata(with:).md)
- [NSData.SearchOptions](foundation/nsdata/searchoptions.md)
