<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSData/range(of:options:in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSData(im)rangeOfData:options:range:"
  },
  "title" : "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.

```
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 [`NSData.SearchOptions`](/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, [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException) is raised.

## Return Value

An [`NSRange`](/documentation/Foundation/NSRange-c.struct) 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.

## Discussion

---

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)