iOS Developer Library

Developer

Foundation Framework Reference NSIndexPath Class Reference

Options
Deployment Target:

On This Page
Language:

NSIndexPath

Inherits From


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later

The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path.

Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node. For example, the index path 1.4.3.2 specifies the path shown in Figure 1.

Figure 1Index path 1.4.3.2 image: ../Art/indexpath.gif
  • Creates an one-node index path.

    Declaration

    Objective-C

    + (instancetype)indexPathWithIndex:(NSUInteger)index

    Parameters

    index

    Index of the item in node 0 to point to.

    Return Value

    One-node index path with index.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later

  • Creates an index path with one or more nodes.

    Declaration

    Objective-C

    + (instancetype)indexPathWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length

    Parameters

    indexes

    Array of indexes to make up the index path.

    length

    Number of nodes to include in the index path.

    Return Value

    Index path with indexes up to length.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later

  • Initializes an allocated NSIndexPath object with a one-node index path.

    Declaration

    Swift

    convenience init(index index: Int)

    Objective-C

    - (instancetype)initWithIndex:(NSUInteger)index

    Parameters

    index

    Index of the item in node 0 to point to.

    Return Value

    Initialized NSIndexPath object representing a one-node index path with index.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Initializes an allocated NSIndexPath object with an index path of a specific length.

    Declaration

    Swift

    init(indexes indexes: UnsafePointer<Int>, length length: Int)

    Objective-C

    - (instancetype)initWithIndexes:(const NSUInteger [])indexes length:(NSUInteger)length

    Parameters

    indexes

    Array of indexes to make up the index path.

    length

    Number of nodes to include in the index path.

    Return Value

    Initialized NSIndexPath object with indexes up to length.

    Discussion

    This method is a designated initializer of NSIndexPath.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • init - init Available in iOS 7.0 through iOS 7.1

    Initializes an allocated NSIndexPath object.

    Declaration

    Objective-C

    - (instancetype)init

    Return Value

    Initialized NSIndexPath object.

    Discussion

    This method is a designated initializer for NSIndexPath.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 7.0 through iOS 7.1

  • Provides the index at a particular node in the index path.

    Declaration

    Swift

    func indexAtPosition(_ node: Int) -> Int

    Objective-C

    - (NSUInteger)indexAtPosition:(NSUInteger)node

    Parameters

    node

    Index value of the desired node. Node numbering starts at zero.

    Return Value

    The index value at node or NSNotFound if the node is outside the range of the index path.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Provides an index path containing the indexes in the receiving index path and another index.

    Declaration

    Swift

    func indexPathByAddingIndex(_ index: Int) -> NSIndexPath

    Objective-C

    - (NSIndexPath *)indexPathByAddingIndex:(NSUInteger)index

    Parameters

    index

    Index to append to the index path’s indexes.

    Return Value

    New NSIndexPath object containing the receiving index path’s indexes and index.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Provides an index path with the indexes in the receiving index path, excluding the last one.

    Declaration

    Swift

    func indexPathByRemovingLastIndex() -> NSIndexPath

    Objective-C

    - (NSIndexPath *)indexPathByRemovingLastIndex

    Return Value

    New index path with the receiving index path’s indexes, excluding the last one.

    Discussion

    Returns an empty NSIndexPath instance if the receiving index path’s length is 1 or less.

    Special Considerations

    On OS X v10.4 this method returns nil when the length of the receiving index path is 1 or less. On iOS and OS X v10.5 and later this method will never return nil.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • length length Property

    The number of indexes in the index path. (read-only)

    Declaration

    Swift

    var length: Int { get }

    Objective-C

    @property(readonly) NSUInteger length

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Copies the objects contained in the index path into indexes.

    Declaration

    Swift

    func getIndexes(_ indexes: UnsafeMutablePointer<Int>)

    Objective-C

    - (void)getIndexes:(NSUInteger *)indexes

    Parameters

    indexes

    Pointer to a C array of objects of size at least the length of the index path. On return, the index path’s indexes.

    Discussion

    It is the developer’s responsibility to allocate the memory for the C array.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Indicates the depth-first traversal order of the receiving index path and another index path.

    Declaration

    Swift

    func compare(_ indexPath: NSIndexPath) -> NSComparisonResult

    Objective-C

    - (NSComparisonResult)compare:(NSIndexPath *)indexPath

    Parameters

    indexPath

    Index path to compare.

    This value must not be nil. If the value is nil, the behavior is undefined.

    Return Value

    The depth-first traversal ordering of the receiving index path and indexPath.

    • NSOrderedAscending: The receiving index path comes before indexPath.

    • NSOrderedDescending: The receiving index path comes after indexPath.

    • NSOrderedSame: The receiving index path and indexPath are the same index path.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later