Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.
- Xcode 10.0+
- Swift Standard Library
A sequence of pairs enumerating the sequence.
This example enumerates the characters of the string “Swift” and prints each character along with its place in the string.
When you enumerate a collection, the integer part of each pair is a counter for the enumeration, but is not necessarily the index of the paired value. These counters can be used as indices only in instances of zero-based, integer-indexed collections, such as
Contiguous. For other collections the counters may be out of range or of the wrong type to use as an index. To iterate over the elements of a collection with its indices, use the
This example iterates over the indices and elements of a set, building a list consisting of indices of names with five or fewer letters.
Now that the
shorter array holds the indices of the shorter names in the
names set, you can use those indices to access elements in the set.