2016-09-22 21 views
15

Gần đây, tôi đã chuyển đổi mã của mình sang Swift 3.0. Phương pháp nguồn dữ liệu chế độ xem và chế độ xem bảng của tôi hiện có chứa IndexPath thay vì NSIndexPath trong chữ ký phương thức của chúng. Nhưng vẫn bên trong định nghĩa phương thức, nó là kiểu đúc IndexPath thành NSIndexPath. tức làNSIndexPath và IndexPath trong 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 
    } 

Bất cứ ai có thể cho tôi biết lý do tại sao indexPath được nhập vào NSIndexPath.

Trả lời

26

Không có lý do gì để đúc IndexPath đến NSIndexPath tại đây. Swift 3 lớp phủ loại IndexPath có tính

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

mà bạn có thể truy cập trực tiếp:

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

Rõ ràng migrator Xcode đã không phải là một công việc hoàn hảo.

Các vấn đề liên quan