Swift 用步幅控制流程
Swift control flow with 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)
}
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]
的区别。
两个步幅函数有什么区别?
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]
的区别。