Discussion 解説
A collection’s indices
property can hold a strong reference to the collection itself, causing the collection to be nonuniquely referenced. If you mutate the collection while iterating over its indices, a strong reference can result in an unexpected copy of the collection. To avoid the unexpected copy, use the index(after:)
method starting with start
to produce indices instead.
あるコレクションのindices
プロパティは、そのコレクション自身に対する強い参照を保持でき、そのコレクションが非特有に参照されるようにします。あなたがコレクションをそれのインデックス全体に反復している間に変化させるならば、ある強い参照はそのコレクションの予期しない複製という結果になります。予期されないコピーを避けるために、代わりにindex(after:)
メソッドを使ってstart
で始めることで、インデックスを生成してください。
var c = MyFancyCollection([10, 20, 30, 40, 50])
var i = c.startIndex
while i != c.endIndex {
c[i] /= 5
i = c.index(after: i)
}
// c == MyFancyCollection([2, 4, 6, 8, 10])
Note 注意
This documentation comment was inherited from Collection
.
この文書化コメントは、Collection
から引き継がれました。