Structure

IndexPath

A list of indexes that together represent the path to a specific location in a tree of nested arrays.

Overview

Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node.

Topics

Creating Index Paths

init()

Creates an empty index path.

init(index: IndexPath.Element)

Creates an index path with a single element.

init(arrayLiteral: IndexPath.Element...)

Creates an index path from an array literal.

init(indexes: Array<IndexPath.Element>)

Creates an index path from an array of elements.

init<ElementSequence>(indexes: ElementSequence)

Creates an index path from a sequence of integers.

typealias IndexPath.Element

A type that represents one node of an index path.

Working with Special Node Names

UIKit and AppKit supply specialized names for the first two index path nodes for use when working with table views and collection views.

var customMirror: Mirror

A mirror that reflects the index path.

var debugDescription: String

A textual description of the index path suitable for debugging.

var description: String

A textual description of the index path.

var endIndex: IndexPath.Index

One past the index of the last node in the index path.

var hashValue: Int

The computed hash value for the index path.

var isEmpty: Bool

A Boolean value indicating whether the collection is empty.

var item: Int

The value of the item element of the index path.

var item: Int

The value of the item element of the index path.

var lazy: LazyCollection<IndexPath>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazySequence<IndexPath>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

var row: Int

The value of the row element of the index path.

var section: Int

The value of the section element of the index path.

var section: Int

The value of the section element of the index path.

var startIndex: IndexPath.Index

The index of the first node in the index path.

Counting Nodes

var isEmpty: Bool

A Boolean value indicating whether the collection is empty.

Accessing Nodes

subscript(IndexPath.Index) -> IndexPath.Element

Accesses one of the index path's nodes.

subscript(Range<IndexPath.Index>) -> IndexPath

Accesses a contiguous subrange of the index path's nodes.

Adding Nodes

static func != (IndexPath, IndexPath) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func + (IndexPath, IndexPath) -> IndexPath

Combines the elements of two index paths into a single index path.

static func += (inout IndexPath, IndexPath)

Appends the elements of another index path to this index path.

static func < (IndexPath, IndexPath) -> Bool

Returns a Boolean that is true if the first index path is less than the second when comparing elements in depth-first traversal order.

static func == (IndexPath, IndexPath) -> Bool

Returns a Boolean that is true if all nodes of the first index path are equal to those of the second in order.

Removing Nodes

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 popFirst() -> Int?

Removes and returns the first element of the collection.

func popLast() -> Int?

Removes and returns the last element of the collection.

Finding Nodes

func max() -> Int?

Returns the maximum element in the sequence.

func min() -> Int?

Returns the minimum element in the sequence.

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() -> IndexPath

Returns a subsequence containing all but the first element of the sequence.

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 enumerated() -> EnumeratedSequence<IndexPath>

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

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.

Excluding Nodes

func dropFirst() -> IndexPath

Returns a subsequence containing all but the first element of the sequence.

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.

Transforming Index Paths

var lazy: LazyCollection<IndexPath>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazySequence<IndexPath>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

Iterating over Nodes

func enumerated() -> EnumeratedSequence<IndexPath>

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

func makeIterator() -> IndexingIterator<IndexPath>

Returns an iterator over the nodes of the index path.

Sorting Nodes

func sorted() -> [Int]

Returns the elements of the sequence, sorted.

func reversed() -> ReversedCollection<IndexPath>

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

Comparing Index Paths

func compare(IndexPath) -> ComparisonResult

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

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.

static func < (IndexPath, IndexPath) -> Bool

Returns a Boolean that is true if the first index path is less than the second when comparing elements in depth-first traversal order.

static func == (IndexPath, IndexPath) -> Bool

Returns a Boolean that is true if all nodes of the first index path are equal to those of the second in order.

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 elementsEqual<OtherSequence>(OtherSequence) -> Bool

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

Manipulating Indexes

typealias IndexPath.Index

A type that points to a particular node in an index path, similar to an array index.

var startIndex: IndexPath.Index

The index of the first node in the index path.

var endIndex: IndexPath.Index

One past the index of the last node in the index path.

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.

typealias IndexPath.Indices

A type that represents a group of nodes in an index path.

Describing an Index Path

var description: String

A textual description of the index path.

var debugDescription: String

A textual description of the index path suitable for debugging.

var customMirror: Mirror

A mirror that reflects the index path.

var hashValue: Int

The computed hash value for the index path.

Using Reference Types

class NSIndexPath

An object containing a list of indexes that bridges to IndexPath; use NSIndexPath when you need reference semantics or other Foundation-specific behavior.

typealias IndexPath.ReferenceType

An alias for this value type's equivalent reference type.

Initializers

init(item: Int, section: Int)

Creates an index path that references an item in a particular section.

init(item: Int, section: Int)

Creates an index path that references an item in a particular section.

init(row: Int, section: Int)

Creates an index path that references a row in a particular section.

Instance Properties

var count: Int

The number of elements in the collection.

var first: Int?

The first element of the collection.

var indices: DefaultIndices<IndexPath>

The indices that are valid for subscripting the collection, in ascending order.

var last: Int?

The last element of the collection.

var underestimatedCount: Int

A value less than or equal to the number of elements in the collection.

var underestimatedCount: Int

A value less than or equal to the number of elements in the sequence, calculated nondestructively.

Instance Methods

func allSatisfy((Int) -> Bool) -> Bool

Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.

func compactMap<ElementOfResult>((Int) -> ElementOfResult?) -> [ElementOfResult]

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func contains(Int) -> Bool

Returns a Boolean value indicating whether the sequence contains the given element.

func contains(where: (Int) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

func drop(while: (Int) -> Bool) -> IndexPath

Returns a subsequence by skipping elements while predicate returns true and returning the remaining elements.

func elementsEqual<OtherSequence>(OtherSequence, by: (Int, OtherSequence.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements in the same order, using the given predicate as the equivalence test.

func filter((Int) -> Bool) -> [Int]

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func first(where: (Int) -> Bool) -> Int?

Returns the first element of the sequence that satisfies the given predicate.

func firstIndex(of: Int) -> Array<Int>.Index?

Returns the first index where the specified value appears in the collection.

func firstIndex(where: (Int) -> Bool) -> Array<Int>.Index?

Returns the first index in which an element of the collection satisfies the given predicate.

func forEach((Int) -> Void)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

func formIndex(inout Array<Int>.Index, offsetBy: Int)

Offsets the given index by the specified distance.

func formIndex(inout Array<Int>.Index, offsetBy: Int, limitedBy: Array<Int>.Index) -> Bool

Offsets the given index by the specified distance, or so that it equals the given limiting index.

func formIndex(after: inout Array<Int>.Index)

Replaces the given index with its successor.

func formIndex(before: inout Array<Int>.Index)

Replaces the given index with its predecessor.

func index(Array<Int>.Index, offsetBy: Int) -> Array<Int>.Index

Returns an index that is the specified distance from the given index.

func index(Array<Int>.Index, offsetBy: Int, limitedBy: Array<Int>.Index) -> Array<Int>.Index?

Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.

func last(where: (Int) -> Bool) -> Int?

Returns the last element of the sequence that satisfies the given predicate.

func lastIndex(of: Int) -> Array<Int>.Index?

Returns the last index where the specified value appears in the collection.

func lastIndex(where: (Int) -> Bool) -> Array<Int>.Index?

Returns the index of the last element in the collection that matches the given predicate.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence, by: (Int, Int) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.

func map<T>((Int) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func map<T>((Int) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func max(by: (Int, Int) -> Bool) -> Int?

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min(by: (Int, Int) -> Bool) -> Int?

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func partition(by: (Int) -> Bool) -> Array<Int>.Index

Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.

func partition(by: (Int) -> Bool) -> Array<Int>.Index

Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.

func prefix(while: (Int) -> Bool) -> IndexPath

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func randomElement() -> Int?

Returns a random element of the collection.

func randomElement<T>(using: inout T) -> Int?

Returns a random element of the collection, using the given generator as a source for randomness.

func reduce<Result>(Result, (Result, Int) -> Result) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func reduce<Result>(into: Result, (inout Result, Int) -> ()) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func reverse()

Reverses the elements of the collection in place.

func shuffle()

Shuffles the collection in place.

func shuffle<T>(using: inout T)

Shuffles the collection in place, using the given generator as a source for randomness.

func shuffled() -> [Int]

Returns the elements of the sequence, shuffled.

func shuffled<T>(using: inout T) -> [Int]

Returns the elements of the sequence, shuffled using the given generator as a source for randomness.

func sort()

Sorts the collection in place.

func sort(by: (Int, Int) -> Bool)

Sorts the collection in place, using the given predicate as the comparison between elements.

func sorted(by: (Int, Int) -> Bool) -> [Int]

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Int) -> Bool) -> [IndexPath]

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

func split(separator: Int, maxSplits: Int, omittingEmptySubsequences: Bool) -> [IndexPath]

Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.

func starts<PossiblePrefix>(with: PossiblePrefix, by: (Int, PossiblePrefix.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.

func swapAt(Array<Int>.Index, Array<Int>.Index)

Exchanges the values at the specified indices of the collection.

Subscripts

subscript(Range<Array<Int>.Index>) -> Slice<IndexPath>

Accesses a contiguous subrange of the collection’s elements.

subscript<R>(R) -> IndexPath

Accesses the contiguous subrange of the collection’s elements specified by a range expression.

Operator Functions

static func ... (IndexPath) -> PartialRangeFrom<IndexPath>

Returns a partial range extending upward from a lower bound.

static func ... (IndexPath) -> PartialRangeThrough<IndexPath>

Returns a partial range up to, and including, its upper bound.

static func ... (IndexPath, IndexPath) -> ClosedRange<IndexPath>

Returns a closed range that contains both of its bounds.

static func ..< (IndexPath) -> PartialRangeUpTo<IndexPath>

Returns a partial range up to, but not including, its upper bound.

static func ..< (IndexPath, IndexPath) -> Range<IndexPath>

Returns a half-open range that contains its lower bound but not its upper bound.

static func <= (IndexPath, IndexPath) -> Bool

Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.

static func > (IndexPath, IndexPath) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.

static func >= (IndexPath, IndexPath) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

See Also

Indexes

struct IndexSet

A collection of unique integer values that represent the indexes of elements in another collection.