繁体   English   中英

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

[英]How convert NSIndexPath to Int in Swift?

我需要UICollectionView的单元格的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了,请参阅Apple的声明:

var row: Int { get }

在类NSIndexPath

只需执行vc.id_category = self.result_category[index.row]["id"].intvalue 只需注意数组index of out bound

希望对您有所帮助。

暂无
暂无

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

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