# IteratorProtocol

{% tabs %}
{% tab title="📗 參考" %}

* Swift in Depth ⟩ [Ch 9:  Iterators, sequences, and collections](https://livebook.manning.com/book/swift-in-depth/chapter-9/)
  {% endtab %}

{% tab title="📘 手冊" %}

* Swift ⟩&#x20;
  * [IteratorProtocol](https://developer.apple.com/documentation/swift/iteratorprotocol)
  * [IndexingIterator\<Element>](https://developer.apple.com/documentation/swift/indexingiterator) - \[Element].[makeIterator](https://developer.apple.com/documentation/swift/sequence/2885155-makeiterator)() 回傳的 iterator
* SwiftDoc ⟩ [IteratorProtocol](https://swiftdoc.org/v5.1/protocol/iteratorprotocol/)
  {% endtab %}
  {% endtabs %}

{% hint style="info" %}

* 具有 **next()** 方法的物件就可遵循 [IteratorProtocol](https://developer.apple.com/documentation/swift/iteratorprotocol)，也就是一個 **iterator**。
* 類似 JS 的 **iterator** protocol。
  {% endhint %}

```swift
public protocol IteratorProtocol {
  associatedtype Element
  mutating func next() -> Element?
}
```
