Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convert NSIndexPath to Int in Swift?

I need the index of a cell of my UICollectionView. I know how get it, but the problem is when I convert this NSIndexpath to Int. This is my code.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let cell = sender as! UICollectionViewCell
    let index = self.collectionView!.indexPathForCell(cell)!

    let vc : VideosViewController = segue.destinationViewController as! VideosViewController
    vc.id_category =  self.result_category[index.row as Int]["id"].intvalue
}
like image 390
BarneyL. BarStin Avatar asked Jul 23 '15 15:07

BarneyL. BarStin


People also ask

How to convert string to INT in Swift?

A variable can be declared as String type by following the below syntax, Swift provides the function of integer initializers using which we can convert a string into an Int type. To handle non-numeric strings, we can use nil coalescing using which the integer initializer returns an optional integer.

What is an indexpath in Swift?

In Swift, an indexPath is a list of indexes that, together, represent the path to a specific location in a tree of nested arrays. It describes an item’s position inside a table view or collection view, storing both its section and its position inside that section.

Is nsindexpath read-only?

NSIndexPath has a read-only structure that contains two Int property sections and rows if you're working with UITableViews or a section and item if you're working with UICollectionViews. And read them by accessing their properties: It's as easy as that!

How to convert a string to integer in iOS?

Sponsor sarunw.com and reach thousands of iOS developers. Int has an initializer that creates a new integer value from the given string. To convert a string to integer, we initialize an Int with desired string. Before you go with these methods, you should know which kind of strings that Int can convert to integer.


1 Answers

You don't need to convert the index.row to Int, it's a Int already, see the declaration of Apple:

var row: Int { get }

In the class NSIndexPath.

Just do vc.id_category = self.result_category[index.row]["id"].intvalue. Just be careful with the index of out bound in the array.

I hope this help you.

like image 87
Victor Sigler Avatar answered Sep 18 '22 03:09

Victor Sigler