Instance Method

suffix(from:)

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

Declaration

func suffix(from start: Int) -> Data

Parameters

start

The index at which to start the resulting subsequence. start must be a valid index of the collection.

Return Value

A subsequence starting at the start position.

Discussion

The following example searches for the index of the number 40 in an array of integers, and then prints the suffix of the array starting at that index:

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

Passing the collection’s endIndex as the start parameter results in an empty subsequence.

print(numbers.suffix(from: numbers.endIndex))
// Prints "[]"

Using the suffix(from:) method is equivalent to using a partial range from the index as the collection’s subscript. The subscript notation is preferred over suffix(from:).

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

Complexity: O(1)

See Also

Selecting Bytes

func filter((UInt8) -> Bool) -> Data

Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.

func prefix(Int) -> Data

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

func prefix(through: Int) -> Data

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

func prefix(upTo: Int) -> Data

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

func prefix(while: (UInt8) -> Bool) -> Data

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func suffix(Int) -> Data

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software