Use of .enumerated() in Swift

Anjali Joshi
Sep 11, 2022

--

enumerated() is used when we want to iterate over each and every item present in a collection or a string. It returns the result in the form of (index, value) where index is the position starting from zero and value is the element at that particular position.

Below are some examples using .enumerated()

  1. enumerated() example used in a String :

Output:

2. enumerated() example used in an Array :

Output:

3. enumerated() example used in a Dictionary :

Output:

If you don’t wish to get the indices of the elements, you can simply print the values in this way:

Example:

Output:

--

--