Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

enumerated
swift
set
community creator

What is the Set.enumerated() function in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Set.enumerated() function in Swift returns a sequence of (index, value) pairs of a set. The index represents an element from a series of consecutive integers starting at zero. The value represents an element in the set.

Syntax

set.enumerated()
The syntax for the enumerated() function in Swift

Parameters

This function takes no parameters.

Return value

The value returned is an enumerated sequence containing pairs of index and elements of a set. The first value of the pair represents the element's index. Its second value represents the element itself.

Example

// create a set
let evenNumbers : Set = [10, 2, 6, 8, 4]
// get the sequence
let sequence = evenNumbers.enumerated()
for (x, y) in sequence{
print(x, y)
}
Using the enumerated() function in Swift

Explanation

  • Line 2: We instantiate the evenNumbers set and initialize it with some values.
  • Line 3: We use the enumerated() function to get an enumerated sequence of pairs. We store the result inside the sequence variable.
  • Line 7: With the for-in loop or the for loop, we are able to print each pair of the sequence to the console.

RELATED TAGS

enumerated
swift
set
community creator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring