swift - 在 Swift 中,什么是 'progression' ?

标签 swift iteration control-flow

根据Control Flow Swift 语言指南 中的部分,

The for-in loop performs a set of statements for each item in a range, sequence, collection, or progression.

我很确定我知道其中三个是什么:

  1. range:用 range operators 定义的东西, .....<
  2. sequence:符合 SequenceType 的东西协议(protocol)(文档在任何地方都不明显,但很多人都有 reverse-engineered 它)
  3. collection:任一 Swift collection types ,即 ArrayDictionary

(我注意到 #3 可能是多余的,因为 ArrayDictionary 似乎都符合 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 than end.

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 to end. Note:: There is no guarantee that end 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/

相关文章:

C++ vector 迭代错误

python - 相当于 Python 在 Ruby 中的 "with"

node.js - 对于需要异步加载大文件才能使用的 Node 模块是否有通用模式?

ios - Swift 数字泛型?

ios - 如何通过 AppDelegate 升级使功能可用于大于 8 的 iOS

javascript - 遍历对象数组并获得新的对象数组

python - 在 Python 迭代期间打印当前值和下一个值

Hadoop 数据和控制流

swift - 在 Swift 的 AFNetworking 2.0 中使用 CDLJSONPResponseSerializer 处理 JSONP

swift - 参数类型不符合预期类型 'WKScriptMessageHandler'