两种步幅函数有什么区别?
步幅(from:to:by)
& (from:through:by)
在完成教程的同时,Control flow with 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)
}
使用 stride(from:through:by:) 代替:
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)
}
最佳答案
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.
注意粗体文本的区别。
这是像 [0...10]
这样的封闭范围之间的区别和一个开放的范围,如 [0..<10]
.
关于跨步快速控制流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49313122/