0

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?

user4671001
  • 309
  • 2
  • 4
  • 12
  • Look at the docs for `NSIndexPath` (the base docs and the `UIKit` category). There are appropriate methods for getting the row, section, item, or other indexes from the `indexPath`. And simply casting is completely wrong. – rmaddy Dec 08 '15 at 22:30
  • NSIndexPath* is a pointer to an object. Casting any pointer to an object including NSIndexPath* to Int will most definitely not give you anything even remotely useful. – gnasher729 Dec 08 '15 at 22:36

2 Answers2

0

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.

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • Your opening sentence is only partially true. You can also create an `NSIndexPath` with any number of indexes. I created a tree view and the index paths can have any number of indexes. – rmaddy Dec 08 '15 at 22:34
  • FYI - an alternate way to write that 1st line of code: `id object = self.dataSource[indexPath.row];`. – rmaddy Dec 08 '15 at 22:43
  • 1
    Correct. This is the alternative `ObjC` syntax. – Julian F. Weinert Dec 08 '15 at 22:44
0

Objective C

NSInteger currentRow = indexPath.row; 

Swift

let currentRow = self.tableView.indexPathForSelectedRow().row
casillas
  • 16,351
  • 19
  • 115
  • 215