在Swift4中按键分组字典数组 - CoreData

时间:2018-01-14 15:43:43

标签: ios core-data grouping swift4

我正在解析我的JSON响应并将其保存到CoreData。我以这种方式使用NSFetchedResultsController获取数据:

func getTablesForOutlet(managedObjectContext:NSManagedObjectContext) -> NSFetchedResultsController<TablesEntity> {
    let fetchedResultController: NSFetchedResultsController<TablesEntity>

    let request: NSFetchRequest<TablesEntity> = TablesEntity.fetchRequest()
    let sort = NSSortDescriptor(key: "section", ascending: true)
    request.sortDescriptors = [sort]

    fetchedResultController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

    do {
        try fetchedResultController.performFetch()
        tablesArray = fetchedResultController.fetchedObjects!


        let predicate = { (element: TablesEntity) in
            return element.section
        }

        let dictionary = Dictionary(grouping: tablesArray, by: predicate)

        tableCollectionView.reloadData()

    }
    catch {
        fatalError("Error in fetching records")
    }


    return fetchedResultController
}

在Swift4中,apple为名为Dictionary的{​​{1}}引入了初始化方法,但它对我的init(grouping:by:)无效。

我在tablesArray行上收到错误Cannot convert value of type '(TablesEntity) -> String?' to expected argument type '(_) -> _

有关如何使这项工作的任何想法? 提前谢谢。

0 个答案:

没有答案