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:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Anjali Joshi
Anjali Joshi

No responses yet

Write a response