Can you downcast a NSIndexPath to NSInterger

64 Views Asked by At

I have a AVPlayer class that I'm using in a detail view that takes a indexPath of type Int.

I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use.

I tried to just simply downcast the NSIndexPath as! NSInteger before sending it to detail VC to use but it crashes without a crash message. So Im a bit stuck as to how to go about this.

Is it possible to downcast this? How can I get the number out of the NSIndexPath into an Int so I can use it in my class?

2

There are 2 best solutions below

3
Julian F. Weinert On

NSIndexPath is always* a combination of section and either row (for UITableView) or item (for UICollectionView).

you need to dissolve the section by accessing the single index:

id object = [[self dataSource] objectAtIndex:[indexPath row]];

* EDIT

Respective the comments: you are right. Multiple indexes are possible with NSIndexPath.

If you know you have multiple indexes (what I wouldn't expect in UITableViewDelegate) you can iterate the indexes:

for (int i = 0; i < [indexPath length]; i++) {
    NSUInteger index = [indexPath indexAtPosition:i];
    // use index to access whatever you want
}

EDIT

PLEASE read documentation and headers before you ask the next time. Little hint: cmd-click NSIndexPath will take you to its header file with all possible informations.

0
casillas On

Objective C

NSInteger currentRow = indexPath.row; 

Swift

let currentRow = self.tableView.indexPathForSelectedRow().row