# 為什么 stride function 在 Swift 中會這樣工作？

[英]Why does stride function work like this in Swift?

``````for i in stride(from: 1, through: 1, by: -1) {
print(i)
}
``````

``````for i in stride(from: 1, through: 2, by: -1) {
print(i)
}
``````

``````start <= x and x <= end
``````

``````start >= x and x >= end
``````

``````for i in stride(from: 0, through: 0, by: 1) { print(i) } // prints 0
for i in stride(from: 0, through: -1, by: 1) { print(i) } // prints nothing

for i in stride(from: 0, through: 0, by: -1) { print(i) } // prints 0
for i in stride(from: 0, through: 1, by: -1) { print(i) } // prints nothing
``````

``````  public mutating func next() -> Element? {
let result = _current.value
if _stride > 0 ? result >= _end : result <= _end {
// Note the `>=` and `<=` operators above. When `result == _end`, the
// following check is needed to prevent advancing `_current` past the
// representable bounds of the `Strideable` type unnecessarily.
//
// If the `Strideable` type is a fixed-width integer, overflowed results
// are represented using a sentinel value for `_current.index`, `Int.min`.
if result == _end && !_didReturnEnd && _current.index != .min {
_didReturnEnd = true
return result
}
return nil
}
_current = Element._step(after: _current, from: _start, by: _stride)
return result
}
``````

``````for i in stride(from: 1, through: 1, by: -1) { print(i) }
``````

``````for i in stride(from: 1, through: 2, by: -1) { print(i) }
``````