Instance Method

enumerated()

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.

Declaration

func enumerated() -> EnumeratedSequence<IndexPath>

Return Value

A sequence of pairs enumerating the sequence.

Discussion

This example enumerates the characters of the string “Swift” and prints each character along with its place in the string.

for (n, c) in "Swift".enumerated() {
    print("\(n): '\(c)'")
}
// Prints "0: 'S'"
// Prints "1: 'w'"
// Prints "2: 'i'"
// Prints "3: 'f'"
// Prints "4: 't'"

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 Array and ContiguousArray. 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 zip(_:_:) function.

This example iterates over the indices and elements of a set, building a list consisting of indices of names with five or fewer letters.

let names: Set = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
var shorterIndices: [Set<String>.Index] = []
for (i, name) in zip(names.indices, names) {
    if name.count <= 5 {
        shorterIndices.append(i)
    }
}

Now that the shorterIndices array holds the indices of the shorter names in the names set, you can use those indices to access elements in the set.

for i in shorterIndices {
    print(names[i])
}
// Prints "Sofia"
// Prints "Mateo"

Complexity: O(1)

See Also

Selecting Nodes

func append(IndexPath)

Appends the nodes of another index path to this one.

func append(Array<IndexPath.Element>)

Appends an array of elements to this index path as additional nodes.

func append(IndexPath.Element)

Appends a single element to this index path as a new node.

func appending(IndexPath.Element) -> IndexPath

Returns a new index path containing the elements of this one plus the given element.

func appending(IndexPath) -> IndexPath

Returns a new index path containing the elements of this one plus those of another index path.

func appending(Array<IndexPath.Element>) -> IndexPath

Returns a new index path containing the elements of this one plus an array of additional elements.

func compare(IndexPath) -> ComparisonResult

Compares this index path to another in depth-first traversal order.

func dropFirst(Int) -> IndexPath

Returns a subsequence containing all but the given number of initial elements.

func dropLast() -> IndexPath

Return a new index path containing all but the last element.

func dropLast(Int) -> IndexPath

Returns a subsequence containing all but the specified number of final elements.

func elementsEqual<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func index(after: IndexPath.Index) -> IndexPath.Index

Returns the index that follows the given index.

func index(before: IndexPath.Index) -> IndexPath.Index

Returns the index that precedes the given index.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

func makeIterator() -> IndexingIterator<IndexPath>

Returns an iterator over the nodes of the index path.

func max() -> Int?

Returns the maximum element in the sequence.

func min() -> Int?

Returns the minimum element in the sequence.

func popFirst() -> Int?

Removes and returns the first element of the collection.

func popLast() -> Int?

Removes and returns the last element of the collection.

func prefix(Int) -> IndexPath

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(through: Array<Int>.Index) -> IndexPath

Returns a subsequence from the start of the collection through the specified position.

func prefix(upTo: Array<Int>.Index) -> IndexPath

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func removeFirst() -> Int

Removes and returns the first element of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func removeLast() -> Int

Removes and returns the last element of the collection.

func removeLast(Int)

Removes the given number of elements from the end of the collection.

func reversed() -> ReversedCollection<IndexPath>

Returns a view presenting the elements of the collection in reverse order.

func sorted() -> [Int]

Returns the elements of the sequence, sorted.

func starts<PossiblePrefix>(with: PossiblePrefix) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func suffix(Int) -> IndexPath

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from: Array<Int>.Index) -> IndexPath

Returns a subsequence from the specified position to the end of the collection.