A type representing continuous, one-dimensional values that can be offset and measured.
SDK
- Xcode 6.0.1+
Framework
- Swift Standard Library
Declaration
Overview
You can use a type that conforms to the Strideable
protocol with the stride(from:
and stride(from:
functions. For example, you can use stride(from:
to iterate over an interval of floating-point values:
The last parameter of these functions is of the associated Stride
type—the type that represents the distance between any two instances of the Strideable
type.
Types that have an integer Stride
can be used as the boundaries of a countable range or as the lower bound of an iterable one-sided range. For example, you can iterate over a range of Int
and use sequence and collection methods.
Conforming to the Strideable Protocol
To add Strideable
conformance to a custom type, choose a Stride
type that can represent the distance between two instances and implement the advanced(by:)
and distance(to:)
methods. For example, this hypothetical Date
type stores its value as the number of days before or after January 1, 2000:
The Stride
type for Date
is Int
, inferred from the parameter and return types of advanced(by:)
and distance(to:)
:
The Date
type can now be used with the stride(from:
and stride(from:
functions and as the bounds of an iterable range.
Important
The Strideable
protocol provides default implementations for the equal-to (==
) and less-than (<
) operators that depend on the Stride
type’s implementations. If a type conforming to Strideable
is its own Stride
type, it must provide concrete implementations of the two operators to avoid infinite recursion.