gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:09:45 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com