Subscript

subscript(_:)

Accesses the element at the specified position. 指定された位置で要素にアクセスします。

Declaration 宣言

subscript(position: ReversedCollection<Base>.Index) -> ReversedCollection<Base>.Element { get }

Parameters パラメータ

position

The position of the element to access. position must be a valid index of the collection that is not equal to the endIndex property. アクセスする要素の位置。positionはコレクションの有効なインデックスで、endIndexプロパティと等しくないものでなければなりません。

Discussion 解説

The following example accesses an element of an array through its subscript to print its value: 以下の例は、配列のある要素にそれの添え字によってアクセスしてそれの値を出力します。


var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
print(streets[1])
// Prints "Bryant"

You can subscript a collection with any valid index other than the collection’s end index. The end index refers to the position one past the last element of a collection, so it doesn’t correspond with an element. あなたは、そのコレクションの末尾インデックスの以外のあらゆる有効なインデックスで、コレクションに対して添え字を使うことができます。末尾インデックスは、あるコレクションの最後の要素を1つ過ぎた位置を参照します、それでそれはある要素に対応しません。

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

Relationships 関係

From Protocol 由来プロトコル