【问题标题】:Swift control flow with stride快速控制流
【发布时间】:2018-03-16 04:14:21
【问题描述】:

两个stride函数有什么区别?

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)
}

【问题讨论】:

  • 在第一个示例中将to 更改为through,在第二个示例中将through 更改为to。注意区别。并阅读文档。注意区别。

标签: swift control-flow stride


【解决方案1】:

stride(from:through:by:):

返回值序列(self, self + stride, self + 2 * stride, … last),其中last是进程中的最后一个值小于或等于end

stride(from:to:by:):

返回值序列(self, self + stride, self + 2 * stride, … last),其中last小于end的进程中的最后一个值。

注意粗体文字的区别。

这是像[0...10] 这样的封闭范围和像[0..<10] 这样的开放范围之间的区别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2018-09-10
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多