2016-09-22 3 views
15

Я недавно преобразовал свой код в Swift 3.0. Мои методы представления данных в виде коллекций и табличного представления теперь содержат IndexPath вместо NSIndexPath в их сигнатуре метода. Но все же внутри определения метода это тип casting IndexPath для NSIndexPath. т.е.NSIndexPath и IndexPath в 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 
    } 

Может кто-нибудь сказать мне, почему 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 Migrator не идеальная работа.

Смежные вопросы