Returns the range of indices described by this range expression within the given collection.
- Xcode 9.3+
- Swift Standard Library
The collection to evaluate this range expression in relation to.
A range suitable for slicing
collection. The returned range is not guaranteed to be inside the bounds of
collection. Callers should apply the same preconditions to the return value as they would to a range provided directly by the user.
You can use the
relative(to:) method to convert a range expression, which could be missing one or both of its endpoints, into a concrete range that is bounded on both sides. The following example uses this method to convert a partial range up to
4 into a half-open range, using an array instance to add the range’s lower bound.
r1 range is bounded on the lower end by
0 because that is the starting index of the
numbers array. When the collection passed to
relative(to:) starts with a different index, that index is used as the lower bound instead. The next example creates a slice of
numbers starting at index
2, and then uses the slice with
relative(to:) to convert
up to a concrete range.
Use this method only if you need the concrete range it produces. To access a slice of a collection using a range expression, use the collection’s generic subscript that uses a range expression as its parameter.