根据Control Flow Swift 语言指南 中的部分,
The
for-in
loop performs a set of statements for each item in a range, sequence, collection, or progression.
我很确定我知道其中三个是什么:
- range:用 range operators 定义的东西,
...
或..<
- sequence:符合
SequenceType
的东西协议(protocol)(文档在任何地方都不明显,但很多人都有 reverse-engineered 它) - collection:任一 Swift collection types ,即
Array
和Dictionary
(我注意到 #3 可能是多余的,因为 Array
和 Dictionary
似乎都符合 SequenceType
。)
但什么是“进步”?是第四件事,还是作者只是啰嗦?
预计到达时间:我看到有一个 CollectionType
协议(protocol)也是如此,所以这解释了#3。
最佳答案
除了 for-in 文档之外,我第一次看到“进展”是在 swift 框架的注释中,其中定义了 stride 方法。
func stride<T : Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression that is less thanend
.
func stride<T : Strideable>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression less than or equal toend
. Note:: There is no guarantee thatend
is an element of the sequence.
所以简而言之,“progression”是指 Strideable
协议(protocol),类似于“collection”是指 CollectionType
协议(protocol) 和类以及符合它的结构。
数字类型(Int、Double、Float、UnsafePointer、Bit 等)倾向于遵循此协议(protocol),因此它们可以在 for in
循环中递增/递减。找到 Strideable
协议(protocol)的完整继承图 here .
关于swift - 在 Swift 中,什么是 'progression' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28711713/