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

index(_:offsetBy:)

Returns an index that is the specified distance from the given index. 与えられたインデックスから指定された隔たりのインデックスを返します。

Declaration 宣言

func index(_ i: ReversedCollection<Base>.Index, offsetBy n: Int) -> ReversedCollection<Base>.Index

Parameters パラメータ

i

A valid index of the collection. そのコレクションの有効なインデックス。

distance

The distance to offset i. distance must not be negative unless the collection conforms to the BidirectionalCollection protocol. iを補う間隔。distanceは、そのコレクションがBidirectionalCollectionプロトコルに準拠するのでない限り、負であってはなりません。

Return Value 戻り値

An index offset by distance from the index i. If distance is positive, this is the same value as the result of distance calls to index(after:). If distance is negative, this is the same value as the result of abs(distance) calls to index(before:). distanceだけインデックスiから補われるインデックス。distanceが正ならば、これはdistanceindex(after:)を呼び出す結果と同じ値です。distanceが負ならば、これはabs(distance)index(before:)を呼び出す結果と同じ値です。

Discussion 解説

The following example obtains an index advanced four positions from a string’s starting index and then prints the character at that position. 以下の例は、文字列の開始インデックスから4位置進んだインデックスを取得して、それからその位置の文字を出力します。


let s = "Swift"
let i = s.index(s.startIndex, offsetBy: 4)
print(s[i])
// Prints "t"

The value passed as distance must not offset i beyond the bounds of the collection. distanceとして渡される値は、このコレクションの境界を越えてiを補ってはなりません。

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(k), where k is the absolute value of distance. 計算量:もしコレクションがRandomAccessCollectionに準拠するならば、O(1);そうでなければ、O(k)、そこでkdistanceの値の絶対値です。

Relationships 関係

From Protocol 由来プロトコル