问题:转换到 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/