跨步快速控制流程

标签 swift control-flow stride

两种步幅函数有什么区别?

步幅(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/

相关文章:

ios - 展开可选值时,Customer Cell 返回 nil

ios - (Swift)状态栏外观UIImagePickerController

Vim:根据缩进/控制流深度改变颜色?

python - 如何从 numpy 中的 dtype 获取步幅?

Swift 2.2 递减特定于 Swift 3 中的循环

numpy - 步幅如何帮助遍历 numpy 中的数组?

ios - 快速在 UICollectionViewCell 中的 UIimage 上设置圆角

Delphi如何在错误时退出到顶级例程

java - 无需两次检查值即可表达此 Java 条件的最简洁方法

swift - Swift 3 混合协议(protocol)、扩展和类继承的可能错误