xcode 无法将 Int 类型的值转换为预期的参数类型 IndexPath(核心数据)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43347232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:54:21  来源:igfitidea点击:

Cannot convert value of type Int to expected argument type IndexPath (Core Data)?

swiftxcodecore-data

提问by konyv12

I have the following code where I get fetchedObjects from a Core Data stack. How can I access the elements of these? It throws an error saying I need an IndexPath?

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

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

    let coordinate2 = fetchedResultsControllerForCoordinateEntity.object(at: 1)

    print(coordinate2)

回答by Manuel

NSFetchedResultsController.objectmethod required an IndexPath. An IndexPathis composed by section and row. If you have only one section you can try with this

NSFetchedResultsController.object方法需要一个IndexPath. AnIndexPath由段和行组成。如果你只有一个部分,你可以试试这个

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