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

# firstRange(of:)

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

## Declaration

```swift
func firstRange<D>(of data: D) -> Range<Self.Index>? where D : DataProtocol
```

## Parameters

- `data`: The data sequence to find.

## Return Value

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

## Discussion

Discussion An example of searching a data buffer converted from a string: let data = "0123456789".data(using: .utf8)! let pattern = "456".data(using: .utf8)! let foundRange = data.firstRange(of: pattern)

// foundRange == Range(4..<7)

## See Also

### Searching Within Data

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