如何在 Swift 中将 NSIndexPath 转换为 Int?

How convert NSIndexPath to Int in Swift?

我需要 UICollectionView 的一个单元格的索引。我知道如何得到它,但问题是当我将此 NSIndexpath 转换为 Int 时。这是我的代码。

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
}

不需要把index.row转成Int,已经是Int了,看苹果的声明:

var row: Int { get }

在classNSIndexPath.

就做vc.id_category = self.result_category[index.row]["id"].intvalue。请注意数组中的 index of out bound

希望对您有所帮助。