Returns a value that is offset the specified distance from this value.
Required. Default implementation provided.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
func advanced(by n: Self.Stride) -> Self
Parameters
n
The distance to advance this value.
Return Value
A value that is offset from this value by 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.
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 Binary
, then for a value x
, a distance n
, and a value y = x
, x
. Using this method with types that have a noninteger Stride
may result in an approximation.
Complexity: O(1)