2016-09-22 90 views
15

我最近將我的代碼轉換爲Swift 3.0。我的收藏視圖和表格視圖數據源方法現在在其方法簽名中包含IndexPath而不是NSIndexPath。但是在方法定義裏面仍然是對NSIndexPath類型轉換IndexPath。即Swift 3.0中的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 
    } 

誰能告訴我,爲什麼indexPath的類型強制轉換爲NSIndexPath

回答

26

這裏沒有理由將IndexPath轉換爲NSIndexPath。 雨燕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 

,您可以直接訪問:

cell.facilityImageName = self.facilityArray[indexPath.row].imageName 
    cell.facilityLabelString = self.facilityArray[indexPath.row].labelText 

顯然Xcode的遷移並沒有一個完美的工作。