Instance Method インスタンスメソッド

suffix(from:)

Returns a subsequence from the specified position to the end of the collection. 指定された位置からコレクションの終わりまでの下位シーケンスを返します。

Declaration 宣言

func suffix(from start: Index) -> Slice<ReversedCollection<Base>>

Parameters パラメータ

start

The index at which to start the resulting subsequence. start must be a valid index of the collection. それで結果の下位シーケンスが始まるインデックス。startはコレクションのひとつの有効なインデックスでなければなりません。

Return Value 戻り値

A subsequence starting at the start position. start位置から始まる下位シーケンス。

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: 以下の例は、整数からなる配列において数40のインデックスを捜して、それからそのインデックスで始まる配列の末尾を出力します。


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. コレクションのendIndexstartパラメータとして渡すことは、空のシーケンスという結果になります。


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:). suffix(from:)メソッドを使うことは、インデックスからの部分的範囲をコレクションのもつ添え字として使うことと同等です。添え字表記法は、suffix(from:)よりも好まれます。


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

Complexity: O(1) 計算量:O(1)