swift - CollectionType 索引跟踪相应的 SequenceType 生成器

标签 swift

SequenceTypeGenerator需要生产 Elements顺序与 Index 相同successor() ?也就是说,以下条件必须适用于所有集合/序列类型吗?

let str = "Hello!"
var dex = str.startIndex
var gen = str.generate()

while dex != str.endIndex { 
  assert (str[dex] == gen.next()) 
  dex = dex.successor()
}

显然是 IndexingGenerator<>将具有此属性,但必须全部 Generators

最佳答案

答案是肯定的。定义 CollectionType 的 Swift 代码中的注释是:

/// The sequence view of the elements is identical to the collection
/// view.  In other words, the following code binds the same series of
/// values to `x` as does `for x in self {}`::
///
///   for i in startIndex..<endIndex {
///     let x = self[i]
///   }

由于for x in self调用了GeneratorType接口(interface),因此通过这两个接口(interface)生成的订单必须相同。新 CollectionTypes 的实现者,充其量似乎是以此为指导。

关于swift - CollectionType 索引跟踪相应的 SequenceType 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641023/

相关文章:

ios - 尝试在 ChartDataEntry 中输入日期/字符串

ios - popToViewRootController swift

ios - 无法在 Xcode 6 Playground 中使用 SpriteKit

ios - 如何修改或更改 PopoverPresentationController 的大小?

ios - 为什么我的备用图标在 iPhone 上有效,但在 iPad 上却无效?

快速将文本颜色更改为深色/浅色模式

ios - 在 iOS 中,如何通过点击 Alert 弹出窗口中的 INSIDE 来关闭 Alert Controller?

swift - 下铸 Nib uitableviewcell

ios - 从另一个类读取/写入 UITextField

swift - 链接到多个网站