转换为 Swift 2 NSIndexPath 错误

Conversion to Swift 2 NSIndexPath error

问题:在转换为 Swift 2 时,我收到以下错误:"Value of optional type [NSIndexPath]? not unwrapped, did you mean to use "!”或“?”。问题是,如果我使用“?”,它会报错说我应该使用“!”,如果我使用“!”它给出了一个错误,说我应该使用“?”。因此它创建了这个似乎无法修复的讨厌的小错误循环。

代码:

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() returns [NSIndexPath]?(可选),您必须为可选链接添加另一个问号并删除 as? NSIndexPath,因为编译器知道解包类型。

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

解决方案:

if let selectedRowIndex = collectionView!.indexPathsForSelectedItems()!.last! as? NSIndexPath

我唯一担心的是类型安全,但它在我的项目中可以正常工作。