Reputation: 246
What is the difference between two stride function?
stride(from:to:by)
& (from:through:by)
While go through the tutorial, Control flow with stride i found that two types of stride function as per my knowledge both working like a same, i don't know what is the difference between them hence anyone can explain?
Use the stride(from:to:by:)
let minutes = 60
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}
Using stride(from:through:by:) instead:
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// render the tick mark every 3 hours (3, 6, 9, 12)
}
Upvotes: 1
Views: 201
Reputation: 318824
stride(from:through:by:)
:
Returns the sequence of values
(self, self + stride, self + 2 * stride, … last)
where last is the last value in the progression less than or equal to end.
stride(from:to:by:)
:
Returns the sequence of values
(self, self + stride, self + 2 * stride, … last)
where last is the last value in the progression that is less than end.
Notice the difference in bold text.
It's the difference between a closed range like [0...10]
and an open range like [0..<10]
.
Upvotes: 3