我发现在Swift 3中使用Sequence/IteratorProtocol找到"工作文档"非常困难.有些教程/文章似乎适用于较旧的Swift.
想象一下玩具双重链表名单DLList......
public class Node
{
// whatever "thing" you have a group of, this is that "thing"
}
public class DLList
{
// toy linked list class here
// so this is a group of "node" in this example
}
Run Code Online (Sandbox Code Playgroud)
我相信以下代表了最简单(?),正确的方法,使其可以,一句话,用DLList在一个for结构.
public class DLList:Sequence
{
// toy linked list class here
public func makeIterator() -> DLListIterator
{
return DLListIterator(self)
}
}
Run Code Online (Sandbox Code Playgroud)
似乎你所要做的就是添加makeIterator呼叫.
IteratorProtocol由于该类是DLList,我们将其称为DLListIterator.看起来似乎是这样
1,你必须有一个"init",基本上是有问题的组类
2,你必须有一个next电话,它必须返回一个与你的小组类神奇相关的"事物". …