Accesses the contiguous subrange of the collection’s elements specified by a range expression.
SDK
- Xcode 11.0+
Framework
- Swift Standard Library
Declaration
Parameters
bounds
A range of the collection’s indices. The bounds of the range must be valid indices of the collection.
Discussion
The range expression is converted to a concrete subrange relative to this collection. For example, using a Partial
range expression with an array accesses the subrange from the start of the range expression until the end of the array.
The accessed slice uses the same indices for the same elements as the original collection uses. This example searches streets
for one of the strings in the slice, and then uses that index in the original array.
Always use the slice’s start
property instead of assuming that its indices start at a particular value. Attempting to access an element by using an index outside the bounds of the slice’s indices may result in a runtime error, even if that index is valid for the original collection.
Complexity: O(1)