---
title: "firstRange(of:in:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/dataprotocol/firstrange(of:in:)"
---

# firstRange(of:in:)

Returns the first found range of the type’s data buffer.

## Declaration

```swift
func firstRange<D, R>(of: D, in: R) -> Range<Self.Index>? where D : DataProtocol, R : RangeExpression, Self.Index == R.Bound
```

## Parameters

- `of`: The data sequence to find.
- `in`: A range to limit the scope of the search.

## Return Value

Return Value The range, if found, of the first match of the provided data sequence.

## Discussion

Discussion An example of searching a constrained range within a data buffer for the first match: let data: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] let pattern: [UInt8] = [2, 3, 4]

let possibleMatch = data.firstRange(of: pattern, in: 5...9) // possibleMatch == nil

let match = data.firstRange(of: pattern, in: 2...9) // match == 2..<5

## See Also

### Searching Within Data

- [firstRange(of:)](foundation/dataprotocol/firstrange(of:).md)
- [lastRange(of:)](foundation/dataprotocol/lastrange(of:).md)
- [lastRange(of:in:)](foundation/dataprotocol/lastrange(of:in:).md)
