Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive Compare with Core Data and Swift

The following code doesn't work:

let sortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector:"caseInsensitiveCompare")

And gives the following error: 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: caseInsensitiveCompare'

Seemed to work fine in Objective-C. Any ideas on why this might be?

Edit: seems like this may not be possible with Core Data/SQLite. In that case, is the best option to get the array and sort it in code afterwards?

like image 896
Gaurav Sharma Avatar asked Feb 02 '16 23:02

Gaurav Sharma


People also ask

Is Swift case sensitive?

Yes, it is a case sensitive language. If you try to reference a variable with the wrong case, you'll get a use of unresolved identifier error. XCode will suggest spellings as you're typing, but doesn't offer a solution after the fact.

What does core data do Swift?

Core Data is a graphical and persistence framework, which is used in Apple devices with operating systems macOS and iOS. Core Data was first introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0.

What is core data in XCode?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


1 Answers

Swift 5, SwiftUI syntax

self._contacts = FetchRequest(
            entity: Contact.entity(),
            sortDescriptors: [
                NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
            ],
            predicate: predicate
        )
like image 139
Michał Ziobro Avatar answered Sep 23 '22 20:09

Michał Ziobro