2016-09-22 14 views
15

Niedawno przekonwertowałem kod na Swift 3.0. Metody źródła danych widoku zbioru i widoku tabeli zawierają teraz IndexPath zamiast NSIndexPath w sygnaturze metody. Ale wciąż wewnątrz definicji metody jest to typ odlewania IndexPath do NSIndexPath. tjNSIndexPath i IndexPath w Swift 3.0

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 
    } 

Czy ktoś może mi powiedzieć, dlaczego indexPath jest typu lanego do NSIndexPath.

Odpowiedz

26

Nie ma powodu, aby oddawać IndexPath do NSIndexPath tutaj. Swift 3 nakładki typu IndexPath ma właściwości

/// 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 

których można uzyskać dostęp bezpośrednio:

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

Widocznie migrator Xcode nie doskonała praca.

Powiązane problemy