Strideable
A type representing continuous, one-dimensional values that can be offset and measured.
protocol Strideable<Stride> : Comparable
You can use a type that conforms to the Strideable
protocol with the stride(from:to:by:)
and stride(from:through:by:)
functions. For example, you can use stride(from:to:by:)
to iterate over an interval of floating-point values:
for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) {
let degrees = Int(radians * 180 / .pi)
print("Degrees: \(degrees), radians: \(radians)")
}
// Degrees: 0, radians: 0.0
// Degrees: 90, radians: 1.5707963267949
// Degrees: 180, radians: 3.14159265358979
// Degrees: 270, radians: 4.71238898038469
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.
var sum = 0
for x in 1...100 {
sum += x
}
// sum == 5050
let digits = (0..<10).map(String.init)
// ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
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:
struct Date: Equatable, CustomStringConvertible {
var daysAfterY2K: Int
var description: String {
// ...
}
}
The Stride
type for Date
is Int
, inferred from the parameter and return types of advanced(by:)
and distance(to:)
:
extension Date: Strideable {
func advanced(by n: Int) -> Date {
var result = self
result.daysAfterY2K += n
return result
}
func distance(to other: Date) -> Int {
return other.daysAfterY2K - self.daysAfterY2K
}
}
The Date
type can now be used with the stride(from:to:by:)
and stride(from:through:by:)
functions and as the bounds of an iterable range.
let startDate = Date(daysAfterY2K: 0) // January 1, 2000
let endDate = Date(daysAfterY2K: 15) // January 16, 2000
for date in stride(from: startDate, to: endDate, by: 7) {
print(date)
}
// January 1, 2000
// January 8, 2000
// January 15, 2000
Supertypes
protocol Comparable
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.protocol Equatable
A type that can be compared for value equality.
Requirements
associatedtype Stride : Comparable, SignedNumeric
A type that represents the distance between two values.
func advanced(by: Self.Stride
) -> Self Returns a value that is offset the specified distance from this value.
func distance(to: Self
) -> Self.Stride Returns the distance from this value to the given value, expressed as a stride.
Citizens in Swift
Members
Subtypes
protocol BinaryFloatingPoint
A radix-2 (binary) floating-point type.
protocol BinaryInteger
An integer type with a binary representation.
protocol FixedWidthInteger
An integer type that uses a fixed size for every instance.
protocol FloatingPoint
A floating-point numeric type.
protocol SignedInteger
An integer type that can represent both positive and negative values.
protocol UnsignedInteger
An integer type that can represent only nonnegative values.
Citizens in Swift
where Self:(redacted)
Members
Extension in Atomics
Subtypes
protocol AtomicInteger
A type that supports atomic integer operations through a separate atomic storage representation.
Extension in RealModule
Subtypes
protocol Real
A type that models the real numbers.