Instance MethodFoundationEssentials
firstRange(of:)
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
Returns the first found range of the type’s data buffer.
func firstRange<D>(of data: D) -> Range<Self.Index>? where D : DataProtocol
Parameters
dataThe data sequence to find.
Return Value
The range, if found, of the first match of the provided data sequence.
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)