无法使用Int类型下标类型'[NSIndexPath]'的值

时间:2021-01-03 22:29:13

So I'm currently converting a project to Swift 2 using Xcode 7 beta, and I am currently getting the error:

所以我目前正在使用Xcode 7 beta将项目转换为Swift 2,我目前收到的错误是:

Cannot subscript a value of type '[NSIndexPath]?' with a type 'Int'

无法下标类型'[NSIndexPath]?'的值'Int'类型

for the following ling of code:

对于以下代码:

let indexPath = indexPaths[0] as! NSIndexPath

when trying to pass data to a view controller when a user selects a cell in the UICollectionView using prepareForSegue method.

当用户使用prepareForSegue方法选择UICollectionView中的单元格时,尝试将数据传递给视图控制器时。

Here is the complete prepareForSegue method. I am unsure if this is a Swift 2 error, but it works fine when using Swift 1.1 for iOS 8.4.

这是完整的prepareForSegue方法。我不确定这是否是Swift 2错误,但在使用Swift 1.1 for iOS 8.4时它可以正常工作。

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "details" {
    let vc = segue.destinationViewController as! DetailsViewController

    let indexPaths = self.collectionView.indexPathsForSelectedItems()
    let indexPath = indexPaths[0] as! NSIndexPath

    let selectedItem = items[indexPath.row]

    vc.selectedItem = selectedItem

    }
}

3 个解决方案

#1


1  

In iOS8 SDK, indexPathsForSelectedItems is declared as:

在iOS8 SDK中,indexPathsForSelectedItems声明为:

func indexPathsForSelectedItems() -> [AnyObject] // returns nil or an array of selected index paths

This was a bug in the SDK because indexPathsForSelectedItems() returns nil when there are no selected items.

这是SDK中的一个错误,因为当没有选定项时,indexPathsForSelectedItems()返回nil。

In iOS9 SDK, that is declared as:

在iOS9 SDK中,声明为:

public func indexPathsForSelectedItems() -> [NSIndexPath]?

2 differences here

2差异在这里

  • The bug was fixed, and returns Optional
  • 该错误已修复,并返回可选

  • returns an array of NSIndexPath instead of AnyObject since Objective-C supports simple Generics.
  • 返回一个NSIndexPath而不是AnyObject的数组,因为Objective-C支持简单的泛型。

So,

  • You have to unwrap it.
  • 你必须打开它。

  • You don't need to cast them to NSIndexPath
  • 您不需要将它们强制转换为NSIndexPath

Try:

 let indexPaths = self.collectionView.indexPathsForSelectedItems()!
 let indexPath = indexPaths[0]

#2


2  

First you should unwrap array then use it so this code should work:

首先你应该解开数组然后使用它,所以这段代码应该工作:

let indexPath = indexPaths?[0] as! NSIndexPath

By the way, avoid using ! to unwrap variables that have chance to be nil or your final app will face runtime crash.

顺便说一句,避免使用!解开有机会成为零的变量或您的最终应用程序将面临运行时崩溃。

#3


0  

You need to unwrap the array but instead of forcing the unwrap with ! it might be safer to use if let to make sure you are not unwrapping nil

你需要解开数组,而不是强制解包!如果让我们确保你不打开零,可能会更安全

if let indexPaths = self.collectionView.indexPathsForSelectedItems() where indexPaths.count > 0{
    let indexPath = indexPaths[0]
}

#1


1  

In iOS8 SDK, indexPathsForSelectedItems is declared as:

在iOS8 SDK中,indexPathsForSelectedItems声明为:

func indexPathsForSelectedItems() -> [AnyObject] // returns nil or an array of selected index paths

This was a bug in the SDK because indexPathsForSelectedItems() returns nil when there are no selected items.

这是SDK中的一个错误,因为当没有选定项时,indexPathsForSelectedItems()返回nil。

In iOS9 SDK, that is declared as:

在iOS9 SDK中,声明为:

public func indexPathsForSelectedItems() -> [NSIndexPath]?

2 differences here

2差异在这里

  • The bug was fixed, and returns Optional
  • 该错误已修复,并返回可选

  • returns an array of NSIndexPath instead of AnyObject since Objective-C supports simple Generics.
  • 返回一个NSIndexPath而不是AnyObject的数组,因为Objective-C支持简单的泛型。

So,

  • You have to unwrap it.
  • 你必须打开它。

  • You don't need to cast them to NSIndexPath
  • 您不需要将它们强制转换为NSIndexPath

Try:

 let indexPaths = self.collectionView.indexPathsForSelectedItems()!
 let indexPath = indexPaths[0]

#2


2  

First you should unwrap array then use it so this code should work:

首先你应该解开数组然后使用它,所以这段代码应该工作:

let indexPath = indexPaths?[0] as! NSIndexPath

By the way, avoid using ! to unwrap variables that have chance to be nil or your final app will face runtime crash.

顺便说一句,避免使用!解开有机会成为零的变量或您的最终应用程序将面临运行时崩溃。

#3


0  

You need to unwrap the array but instead of forcing the unwrap with ! it might be safer to use if let to make sure you are not unwrapping nil

你需要解开数组,而不是强制解包!如果让我们确保你不打开零,可能会更安全

if let indexPaths = self.collectionView.indexPathsForSelectedItems() where indexPaths.count > 0{
    let indexPath = indexPaths[0]
}