CollectionType索引跟踪对应的SequenceType生成器

时间:2015-04-15 03:17:29

标签: swift

SequenceType Generator是否需要以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

1 个答案:

答案 0 :(得分:0)

答案是'是'。定义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接口,因此通过两个接口生成的订单必须相同。新的CollectionTypes的实现者似乎最多只能将其作为指导。