简体   繁体   English

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

[英]How convert NSIndexPath to Int in Swift?

I need the index of a cell of my UICollectionView . 我需要UICollectionView的单元格的UICollectionView I know how get it, but the problem is when I convert this NSIndexpath to Int . 我知道如何获得它,但是问题是当我将此NSIndexpath转换为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
}

You don't need to convert the index.row to Int , it's a Int already, see the declaration of Apple: 您不需要将index.row转换为Int ,它已经是一个Int了,请参阅Apple的声明:

var row: Int { get }

In the class NSIndexPath . 在类NSIndexPath

Just do vc.id_category = self.result_category[index.row]["id"].intvalue . 只需执行vc.id_category = self.result_category[index.row]["id"].intvalue Just be careful with the index of out bound in the array. 只需注意数组index of out bound

I hope this help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM