Swift - FetchRequest 自定义排序(函数) [英] Swift - FetchRequest custom sorting (function)

查看:40
本文介绍了Swift - FetchRequest 自定义排序(函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将实体(来自 CoreData)分类到特定的构建数组中.

I was wondering how to sort entities (from CoreData) into specific built up arrays.

假设我将以下用户添加到 CoreData:

Let's say I have the following users added to CoreData:

Peter Smith
Emily Hughes
Grace Scott
Peter Brown
Ruby Turner
Chloe Scott
James Simpson

目前,它是按姓氏的字母顺序排序的:

Currently, It's sorted alphabetically by it's family name:

@FetchRequest(
    entity: User.entity(),
    sortDescriptors: [NSSortDescriptor(keyPath: \User.lastname, ascending: true)]
) var users: FetchedResults<User>

// Ends up in (for better representation as JSON):
[
    "Peter Brown",
    "Emily Hughes"
    "Grace Scott"
    "Chloe Scott"
    "James Simpson"
    "Peter Smith"
    "Ruby Turner"
]

到目前为止,一切都很好.但我想以不同的方式对其进行排序,不幸的是,我没有在互联网上找到任何解决方案.这就是我想要的排序方式(首先,按姓氏排序,然后按名字排序):

So far, so good. But I want to sort it on a different way and unfortunately, I did not found any solution on the internet. This is how I want to sort it (first, sorted by family name and second by firstname):

{
    "b": ["Peter Brown"],
    "h": ["Emily Hughes"],
    "s": ["Chloe Scott", "Grace Scott", "James Simpson", "Peter Smith"],
    "t": ["Ruby Turner"]
}

你们中有人知道如何做到这一点吗?也许如何使用自定义函数对从 CoreData 获取的数据进行排序?

Does anyone of you have an idea how to do this? Maybe how to use a custom function for sorting data fetched from CoreData?

先谢谢你!

推荐答案

要将其更改为字典,您只需将其缩减即可.

To change it to a dictionary you can just reduce it.

    let sortedForJson = namesArray.reduce(into: [String:[String]](), {
        let firstLetter = String( $1[ $1.index(after: $1.firstIndex(of: " ")!) ] )
        $0[ firstLetter, default: [String]()].append($1)
        $0[firstLetter] = $0[firstLetter]?.sorted()
    })

如果你想对它进行排序以在视图中显示,即:在表格视图上有节名称,你需要先按节标题排序.因此,如果您想要姓氏的第一个字母,您可以保留排序描述符,并将第二个添加到第一个名字的数组中,然后将节标题通知获取的结果控制器.

If you want to sort it for display in the view ie: have section names on a table view, you will need to sort by the section title first. So if you want the first letter of the last name you would keep your sort descriptor and add a second one to the array for the first name, then inform the fetched results controller of the section title.

       @FetchRequest(
entity: User.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \User.lastname, ascending: true),NSSortDescriptor(keyPath: \User.firstName, ascending: true)]

) 变量用户:FetchedResults

) var users: FetchedResults

查看文档以显示部分索引标题名称https://developer.apple.com/documentation/coredata/nsfetchedresultscontroller

see docs for displaying section index title name https://developer.apple.com/documentation/coredata/nsfetchedresultscontroller

在这种情况下,您只希望第一个字母成为我认为的标题.

In this case you only want the first letter to be the title I think so.

     override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          guard let sectionInfo = <#Fetched results controller#>?.sections?[section] else {
          return nil
      }
      return String( sectionInfo.name.first! )
    }

这篇关于Swift - FetchRequest 自定义排序(函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆