简体   繁体   English

您可以将NSIndexPath转换为NSInterger吗

[英]Can you downcast a NSIndexPath to NSInterger

I have a AVPlayer class that I'm using in a detail view that takes a indexPath of type Int. 我在详细视图中使用的AVPlayer类采用了Int类型的indexPath。

I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use. 我从表视图tableViewDidSelectAtIndexPath获取indexPath,然后将其发送到详细信息VC以使用。

I tried to just simply downcast the NSIndexPath as! 我试图只是简单地将NSIndexPath转换为! NSInteger before sending it to detail VC to use but it crashes without a crash message. NSInteger在将其发送给详细VC之前可以使用,但是它崩溃时没有崩溃消息。 So Im a bit stuck as to how to go about this. 因此,我对如何执行此操作有些困惑。

Is it possible to downcast this? 有可能对此cast之以鼻吗? How can I get the number out of the NSIndexPath into an Int so I can use it in my class? 如何将NSIndexPath中的数字获取到Int中以便可以在班级中使用它?

NSIndexPath is always* a combination of section and either row (for UITableView ) or item (for UICollectionView ). NSIndexPath始终*是 (对于UITableView )或 (对于UICollectionView )的组合。

you need to dissolve the section by accessing the single index: 您需要通过访问单个索引来分解该部分:

id object = [[self dataSource] objectAtIndex:[indexPath row]];

* EDIT *编辑

Respective the comments: you are right. 分别评论:您是对的。 Multiple indexes are possible with NSIndexPath . NSIndexPath可以使用多个索引。

If you know you have multiple indexes (what I wouldn't expect in UITableViewDelegate ) you can iterate the indexes: 如果您知道有多个索引(在UITableViewDelegate是我所不希望的),则可以迭代索引:

for (int i = 0; i < [indexPath length]; i++) {
    NSUInteger index = [indexPath indexAtPosition:i];
    // use index to access whatever you want
}

EDIT 编辑

PLEASE read documentation and headers before you ask the next time. 在下次询问之前, 阅读文档和标题。 Little hint: cmd - click NSIndexPath will take you to its header file with all possible informations. 小提示: cmd- 单击 NSIndexPath将带您进入带有所有可能信息的头文件。

Objective C 目标C

NSInteger currentRow = indexPath.row; 

Swift 迅速

let currentRow = self.tableView.indexPathForSelectedRow().row

暂无
暂无

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

相关问题 Swift 2.0错误来自'[NSIndexPath]?'到'[indexpath]'只能解包可选 - Swift 2.0 error Downcast from '[NSIndexPath]?' to '[indexpath]' only unwraps optional Swift:如何将NSIndexPath绑定到addTarget - Swift: How can I bind an NSIndexPath to a addTarget 使用NSInterger值对NSMuatbleArray进行排序 - Sorting NSMuatbleArray with NSInterger values 不能在`storyboard.instantiateViewController`之后下调 - Can't downcast after `storyboard.instantiateViewController` 如何判断 UITableView 是否包含特定的 NSIndexPath? - How can I tell if a UITableView contains a specific NSIndexPath? 我可以在“ for in”控制结构中向下转换Swift吗? - Can I downcast in Swift in a “for in” control structure? 斯威夫特 - &#39;AnyObject!&#39; 不能转换为&#39;ViewController&#39;; 你的意思是用&#39;as!&#39; 迫使低垂? - Swift - 'AnyObject!' is not convertible to 'ViewController'; did you mean to use 'as!' to force downcast? “ ValidationError”不能转换为“ Error”;您是要使用“ as!”吗? 强迫垂头丧气? - 'ValidationError' is not convertible to 'Error';did you mean to use 'as!' to force downcast? 我们可以在NSIndexPath对象中存储什么类型的数据 - what type of data we can store in NSIndexPath object &#39;NSString的!&#39; 不能转换为&#39;String&#39;; 您是要使用“ as!”吗? 强迫垂头丧气? - 'NSString!' is not convertible to 'String'; did you mean to use 'as!' to force downcast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM