NSIndexPath Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
NSIndexPath.h
Related sample code

Overview

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 1  Index path 1.4.3.2
Index path “1.4.3.2”

Adopted Protocols

NSCoding
NSCopying

Tasks

Creating Index Paths

Querying Index Paths

Comparing Index Paths

New Methods

Class Methods

indexPathWithIndex:

Creates an one-node index path.

+ (instancetype)indexPathWithIndex:(NSUInteger)index
Parameters
index

Index of the item in node 0 to point to.

Return Value

One-node index path with index.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

indexPathWithIndexes:length:

Creates an index path with one or more nodes.

+ (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.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
NSIndexPath.h

Instance Methods

compare:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

getIndexes:

Copies the objects contained in the index path into indexes.

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

indexAtPosition:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
NSIndexPath.h

indexPathByAddingIndex:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

indexPathByRemovingLastIndex

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

init

Initializes an allocated NSIndexPath object.

- (instancetype)init
Return Value

Initialized NSIndexPath object.

Discussion

This method is a designated initializer for NSIndexPath.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSIndexPath.h

init

- (instancetype)init
Return Value

Discussion

Availability
  • Available in iOS 7.0 and later.
Declared In
NSIndexPath.h

initWithIndex:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

initWithIndexes:length:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h

length

Provides the number of indexes in the index path.

- (NSUInteger)length
Return Value

Number of indexes in the index path.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSIndexPath.h