是 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
?
最佳答案
答案是肯定的。定义 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/