<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/AnyIterator/init(_:)-3m1u6",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11AnyIteratorVyAByxGqd__c7ElementQyd__RszStRd__lufc"
  },
  "title" : "init(_:)"
}
-->

# init(_:)

Creates an iterator that wraps a base iterator but whose type depends
only on the base iterator’s element type.

```
init<I>(_ base: I) where Element == I.Element, I : IteratorProtocol
```

## Parameters

`base`

An iterator to type-erase.

## Discussion

You can use `AnyIterator` to hide the type signature of a more complex
iterator. For example, the `digits()` function in the following example
creates an iterator over a collection that lazily maps the elements of a
`Range<Int>` instance to strings. Instead of returning an
iterator with a type that encapsulates the implementation of the
collection, the `digits()` function first wraps the iterator in an
`AnyIterator` instance.

```
func digits() -> AnyIterator<String> {
    let lazyStrings = (0..<10).lazy.map { String($0) }
    let iterator:
        LazyMapSequence<Range<Int>, String>.Iterator
        = lazyStrings.makeIterator()

    return AnyIterator(iterator)
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)