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
?
答案 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的实现者似乎最多只能将其作为指导。