无法将Int类型的值转换为预期的参数类型IndexPath(Core Data)?

时间:2017-04-11 13:22:57

标签: swift xcode core-data

我有以下代码,我从Core Data堆栈获取fetchedObjects。我如何访问这些元素?它抛出一个错误,说我需要一个IndexPath?

func loadDataToMapView() {
    guard let mapCoordinates = fetchedResultsControllerForCoordinateEntity.fetchedObjects else { return 0 }

    let coordinate2 = fetchedResultsControllerForCoordinateEntity.object(at: 1)

    print(coordinate2)

1 个答案:

答案 0 :(得分:9)

NSFetchedResultsController.object方法需要一个IndexPath。 IndexPath由section和row组成。如果您只是一个部分,可以尝试使用此

let myIndexPath = IndexPath(row: 1, section: 0)
let coordinate2 = fetchedResultsControllerForCoordinateEntity.object(at: myIndexPath)