Tarun
Tarun

Reputation: 766

How to convert NSIndexpath into NSInteger or simply int?

I need to convert a nsindexpath var into NsInteger or simply an int. e.g:

 int rowIndex = [mGoogleBaseTable selectedRow];
 //mGoogleBaseTable is a NSTable type in Macdevelopement..

&&

int rowIndex = [mGoogleBaseTable indexPathForSelectedRow];
//mGoogleBaseTable is a UITableView type in Iphone (iOS)Development.

selectedRow returns NSInteger simply but indexPathForSelectedRow returns a NSIndexPath..

Please help me how can i achieve this.

Upvotes: 52

Views: 59335

Answers (6)

Pavitha
Pavitha

Reputation: 181

Simply convert into int by,

NSIndexPath *path = ... // some indexpath

int row = (int)path.row;

Upvotes: 1

King-Wizard
King-Wizard

Reputation: 15694

In Swift:

    let section: Int = indexPath.section
    let row: Int = indexPath.row

Upvotes: 8

Wiza
Wiza

Reputation: 1

In swift :

  1. Associate your collection view to your var :

    @IBOutlet weak var collectionView: UICollectionView!

  2. In prepareForSegue func, you can call :

    var indexPath = collectionView.indexPathsForSelectedItems()

Upvotes: -1

PS.OHM
PS.OHM

Reputation: 483

Maybe, this is can help you

NSInteger variable = indexPath.row;

Upvotes: 14

verma
verma

Reputation: 753

I prefer doing it this way

NSNumber *selRow = [[NSNumber alloc] initWithInteger:indexPath.row];

Upvotes: 7

mvds
mvds

Reputation: 47094

If

NSIndexPath *p = ... // some indexpath

then

NSInteger row = p.row;
NSInteger section = p.section;

That's all!

Upvotes: 132

Related Questions