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

advanced(by:)

Returns a value that is offset the specified distance from this value. 指定の隔たりをこの値からオフセットされたある値を返します。

Declaration 宣言

func advanced(by n: Int) -> UnsafeMutableRawPointer

Parameters パラメータ

n

The distance to advance this value. この値を前進させる距離。

Return Value 戻り値

A value that is offset from this value by n. この値からnをオフセットされる値。

Discussion 解説

Use the advanced(by:) method in generic code to offset a value by a specified distance. If you’re working directly with numeric values, use the addition operator (+) instead of this method. advanced(by:)メソッドを総称体コードにおいて使用して、ある値を指定された隔たりだけオフセットしてください。あなたが直接に数値を扱っているならば、加算演算子(+)をこのメソッドの代わりに使ってください。


func addOne<T: Strideable>(to x: T) -> T
    where T.Stride: ExpressibleByIntegerLiteral
{
    return x.advanced(by: 1)
}


let x = addOne(to: 5)
// x == 6
let y = addOne(to: 3.5)
// y = 4.5

If this type’s Stride type conforms to BinaryInteger, then for a value x, a distance n, and a value y = x.advanced(by: n), x.distance(to: y) == n. Using this method with types that have a noninteger Stride may result in an approximation. If the result of advancing by n is not representable as a value of this type, then a runtime error may occur. この型のもつStride型がBinaryIntegerに準拠するならば、そのとき値x、隔たりn、そして値y = x.advanced(by: n)に対して、x.distance(to: y) == n。このメソッドを非整数Strideを持つ型とともに使うことは、近似値という結果になるかもしれません。nだけ前進する結果がこの型の値として表現可能でないならば、実行時エラーが起こるでしょう。

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

Relationships 関係

From Protocol 由来プロトコル