Swift 4.2 中的 NSSortDescriptor 区分大小写

NSSortDescriptor case sensitivity in Swift 4.2

我正在尝试使用 NSSortDescriptor 不区分大小写按字母顺序排序 Swift 4.2。不幸的是,我只在 Objective-C 中找到了解决方案。

func fetchRequest() -> NSFetchRequest<NSFetchRequestResult> {

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
    let sorter = NSSortDescriptor(key: "texttitle", ascending: true)

    fetchRequest.sortDescriptors = [sorter]
    return fetchRequest

}

正在使用 caseInsensitiveCompare:(带冒号)在 Swift 4.2.

中不起作用

这个有效:

func fetchRequest() -> NSFetchRequest<NSFetchRequestResult> {

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
    let sorter = NSSortDescriptor(key: "texttitle", ascending: true, selector: #selector(NSString.caseInsensitiveCompare))

    fetchRequest.sortDescriptors = [sorter]
    return fetchRequest

}