Swift 3.0中的NSIndexPath和IndexPath

PGD*_*Dev 16 ios uicollectionview swift swift3

我最近将我的代码转换为Swift 3.0.我的集合视图和表视图数据源方法现在包含IndexPath而不是NSIndexPath在其方法签名中.但仍然在方法定义中,它是对NSIndexPath的类型转换IndexPath.即

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell : NNAmenitiesOrFurnishingCollectionViewCell = self.amenitiesOrFurnishingCollectionView.dequeueReusableCell(withReuseIdentifier: "NNAmenitiesOrFurnishingCollectionViewCell", for: indexPath) as! NNAmenitiesOrFurnishingCollectionViewCell
        cell.facilityImageName = self.facilityArray[(indexPath as NSIndexPath).row].imageName
        cell.facilityLabelString = self.facilityArray[(indexPath as NSIndexPath).row].labelText
        return cell
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我为什么indexPath类型铸造NSIndexPath.

Mar*_*n R 27

没有理由投IndexPathNSIndexPath这里.Swift 3覆盖类型IndexPath具有属性

/// The section of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var section: Int

/// The row of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var row: Int
Run Code Online (Sandbox Code Playgroud)

你可以直接访问:

    cell.facilityImageName = self.facilityArray[indexPath.row].imageName
    cell.facilityLabelString = self.facilityArray[indexPath.row].labelText
Run Code Online (Sandbox Code Playgroud)

显然Xcode迁移器并不是一个完美的工作.

  • 进行转换的原因是,如果要将IndexPath发送到需要NSIndexPath作为参数的函数,或者至少需要在Swift 4中进行转换。 (2认同)