<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Strideable",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sx"
  },
  "title" : "Strideable"
}
-->

# Strideable

A type representing continuous, one-dimensional values that can be offset
and measured.

```
protocol Strideable<Stride> : Comparable
```

## Overview

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
```

> 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.

## Topics

### Getting an Offset Value

[`advanced(by:)`](/documentation/Swift/Strideable/advanced(by:))

Returns a value that is offset the specified distance from this value.

[`+(_:_:)`](/documentation/Swift/Strideable/+(_:_:)-8m1px)

[`+(_:_:)`](/documentation/Swift/Strideable/+(_:_:)-94mlm)

[`-(_:_:)`](/documentation/Swift/Strideable/-(_:_:)-1kjns)

[`-(_:_:)`](/documentation/Swift/Strideable/-(_:_:)-2mot7)

### Comparing Values

[`distance(to:)`](/documentation/Swift/Strideable/distance(to:))

Returns the distance from this value to the given value, expressed as a
stride.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)