不区分大小写与核心数据和Swift进行比较 [英] Case Insensitive Compare with Core Data and Swift
问题描述
以下代码不起作用:
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector:"caseInsensitiveCompare")
并给出以下错误:
'NSInvalidArgumentException',原因:'不受支持的NSSortDescriptor选择器:caseInsensitiveCompare'
And gives the following error: 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: caseInsensitiveCompare'
在Objective-C中似乎可以正常工作。
Seemed to work fine in Objective-C. Any ideas on why this might be?
编辑:似乎对于Core Data / SQLite来说不可能。在这种情况下,获取数组并将其按代码排序的最佳选择是什么?
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?
推荐答案
选择器为 caseInsensitiveCompare:
,带有冒号,您可以将其与Core Data一起使用。
The selector is caseInsensitiveCompare:
, with the colon, and you can use it with Core Data.
使用冒号是因为方法(是 NSString
的实例方法,它接受一个参数(要比较的字符串)。
The colon is there because the method (which is an instance method of NSString
) takes one argument (the string to compare with).
作为可与Core Data和SQLite
存储一起使用的排序选择器列表,可在
核心数据编程指南中的//developer.apple.com/library/tvos/documentation/Cocoa/Conceptual/CoreData/PersistentStoreFeatures.html rel = noreferrer>持久性存储类型和行为 em>。
As list of sort selectors which can be used with Core Data and a SQLite store can be found in Persistent Store Types and Behaviors in the Core Data Programming Guide.
这篇关于不区分大小写与核心数据和Swift进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!