swift - 转换为 Swift 2 NSIndexPath 错误

标签 swift uicollectionviewlayout nsindexpath

问题:转换到 Swift 2 后,我收到以下错误:“可选类型 [NSIndexPath] 的值?未展开,您是要使用“!”还是“?””。问题是,如果我使用“?”,它会给出一个错误,提示我应该使用“!”,如果我使用“!”它给出了一个错误,说我应该使用“?”。因此,它创建了这个令人讨厌的小错误循环,似乎无法修复。

代码:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {

    if identifier == Constants.SegueIdentifier {
        if let selectedRowIndex = collectionView?.indexPathsForSelectedItems().last as? NSIndexPath {
            if let cell = collectionView?.cellForItemAtIndexPath(selectedRowIndex) {
                //We check if the selected Card is the one in the middle to open the chat. If it's not, we scroll to the side card selected.
                if cell.frame.size.height > cell.bounds.size.height {
                    return true
                } else {
                    collectionView?.scrollToItemAtIndexPath(selectedRowIndex, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
                    return false
                }
            }
        }
    }

    return true
}

我无法想出任何解决方法,因为我似乎需要以某种方式打开它。有人见过这个问题吗?

最佳答案

indexPathsForSelectedItems() 返回 [NSIndexPath]? (可选),您必须为可选链接添加另一个问号并删除 as? NSIndexPath 因为编译器知道未包装的类型。

if let selectedRowIndex = collectionView?.indexPathsForSelectedItems()?.last {

关于swift - 转换为 Swift 2 NSIndexPath 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34752376/

相关文章:

macos - 使用字典查找和强制转换可选绑定(bind)时出现 Swift 编译器错误

ios - 用于文本字段输入的 Swift UIDatePicker - 如何使 UIDatePicker 消失?

ios - 尽管宽度相等,但 Collection View 网格最后一行显示不同 - swift iOS

ios - 如何根据内容设置 collectionview 单元格宽度?

swift - 在 Collection View 之外有一个按钮来执行 Collection View 单元格应该执行的操作

ios - Storyboard 不接受 UIView 的某些尺寸

ios - 更改 Realm 对象会使应用程序崩溃

ios - UICollectionViewFlowLayout Voice Over 使用流布局读取无序的项目

ios - 如果在另一个表更新后调用得太快,tableView.reloadRows 会闪烁

ios - 当前索引与错误数据关联