Interface

# Iterable

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

The <span class="title-ref">Iterable</span> interface allows objects to denote themselves as being able to be used with "For Each" loops and provides a way to create Iterators, which do the actual work.

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                          | Parameters | Returns                            | Shared |
|-------------------------------|------------|------------------------------------|--------|
| `Iterator<iterable.iterator>` |            | `Iterator</api/language/iterator>` |        |

## Method descriptions

<div id="iterable.iterator">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Iterable.Iterator

**Iterator** As `Iterator</api/language/iterator>`

> Returns an Iterator that can be used with `For Each...Next</api/language/loops/for_each...next>` loops.
>
> Classes that implement <span class="title-ref">Iterable</span>, such as Dictionary, can be used with For..Each loops:
>
> ``` xojo
> Var dict As New Dictionary
>
> dict.Value("First Key") = "First Value"
> dict.Value("Second Key") = "Second Value"
>
> For Each entry As DictionaryEntry In dict
>   TextArea1.Text = TextArea1.Text + " " + entry.Key + " " + entry.Value
> Next
> ```
>
> Note that if you change an <span class="title-ref">Iterable</span> while iterating over it using For Each...Next, an exception is raised. If you find you need to iterate and change the data, you can add a method to get all the keys into an array and then iterate through the array to access the values. A method could be something like this:
>
> ``` xojo
> Function EagerlyEvaluateIterable(obj As Iterable) As Variant()
>   Var results() As Variant
>   For Each item As Variant In obj
>     results.Add(item)
>   Next
>   Return results
> End Function
> ```
>
> Now you can call the method to get an array where you can then modify the contents. For example. this would let you iterate and modify a Dictionary:
>
> ``` xojo
> For Each entry As DictionaryEntry In EagerlyEvaluateIterable(d)
>   ' Stuff that can mutate the dictionary
> Next
> ```
>
> You can also use FolderItem.Children to loop through the files in a folder:
>
> ``` xojo
> Var docs As FolderItem
> docs = Special.Folder.Documents
>
> Var files() As String
> For Each f As FolderItem In docs.Children
>   files.Add(f.Name)
> Next
> ```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Iterator</api/language/iterator>` interface, `DatabaseRow</api/databases/databaserow>`, `Dictionary</api/language/dictionary>`, `RowSet</api/databases/rowset>`, `Set</api/language/set>`

</div>
