Instance Method

prefix(through:)

Returns a subsequence from the start of the collection through the specified position.

Declaration

func prefix(through position: Array<Int>.Index) -> IndexPath

Parameters

end

The index of the last element to include in the resulting subsequence. end must be a valid index of the collection that is not equal to the endIndex property.

Return Value

A subsequence up to, and including, the end position.

Discussion

The resulting subsequence includes the element at the position end. The following example searches for the index of the number 40 in an array of integers, and then prints the prefix of the array up to, and including, that index:

let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.prefix(through: i))
}
// Prints "[10, 20, 30, 40]"

Using the prefix(through:) method is equivalent to using a partial closed range as the collection’s subscript. The subscript notation is preferred over prefix(through:).

if let i = numbers.firstIndex(of: 40) {
    print(numbers[...i])
}
// Prints "[10, 20, 30, 40]"

Complexity: O(1)

See Also

Selecting Nodes

func append(IndexPath)

Appends the nodes of another index path to this one.

func append(Array<IndexPath.Element>)

Appends an array of elements to this index path as additional nodes.

func append(IndexPath.Element)

Appends a single element to this index path as a new node.

func appending(IndexPath.Element) -> IndexPath

Returns a new index path containing the elements of this one plus the given element.

func appending(IndexPath) -> IndexPath

Returns a new index path containing the elements of this one plus those of another index path.

func appending(Array<IndexPath.Element>) -> IndexPath

Returns a new index path containing the elements of this one plus an array of additional elements.

func compare(IndexPath) -> ComparisonResult

Compares this index path to another in depth-first traversal order.

func dropFirst(Int) -> IndexPath

Returns a subsequence containing all but the given number of initial elements.

func dropLast() -> IndexPath

Return a new index path containing all but the last element.

func dropLast(Int) -> IndexPath

Returns a subsequence containing all but the specified number of final elements.

func elementsEqual<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func enumerated() -> EnumeratedSequence<IndexPath>

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.

func index(after: IndexPath.Index) -> IndexPath.Index

Returns the index that follows the given index.

func index(before: IndexPath.Index) -> IndexPath.Index

Returns the index that precedes the given index.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

func makeIterator() -> IndexingIterator<IndexPath>

Returns an iterator over the nodes of the index path.

func max() -> Int?

Returns the maximum element in the sequence.

func min() -> Int?

Returns the minimum element in the sequence.

func popFirst() -> Int?

Removes and returns the first element of the collection.

func popLast() -> Int?

Removes and returns the last element of the collection.

func prefix(Int) -> IndexPath

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(upTo: Array<Int>.Index) -> IndexPath

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func removeFirst() -> Int

Removes and returns the first element of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func removeLast() -> Int

Removes and returns the last element of the collection.

func removeLast(Int)

Removes the given number of elements from the end of the collection.

func reversed() -> ReversedCollection<IndexPath>

Returns a view presenting the elements of the collection in reverse order.

func sorted() -> [Int]

Returns the elements of the sequence, sorted.

func starts<PossiblePrefix>(with: PossiblePrefix) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func suffix(Int) -> IndexPath

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from: Array<Int>.Index) -> IndexPath

Returns a subsequence from the specified position to the end of the collection.