NSIndexPath и IndexPath в Swift 3.0
Недавно я преобразовал свой код в Swift 3.0. Теперь мои методы представления данных в виде коллекции и таблицы видят IndexPath
вместо NSIndexPath
в своей сигнатуре метода. Но все же внутри определения метода это тип casting IndexPath для NSIndexPath. то есть.
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
.
Ответы
Ответ 1
Здесь нет причин бросать IndexPath
в NSIndexPath
.
Тип верхнего слоя 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
с которым вы можете напрямую обращаться:
cell.facilityImageName = self.facilityArray[indexPath.row].imageName
cell.facilityLabelString = self.facilityArray[indexPath.row].labelText
По-видимому, Xcode migrator не был идеальным решением.