NSSortDescriptor 和 [__NSCFNumber localizedStandardCompare:]:发送到实例的无法识别的选择器

NSSortDescriptor and [__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance

我正在使用排序描述符对 NSManagedObject (CoreData) 数组进行排序。

我有两个 Int16 值的排序描述符(今天添加了第二个)。

出于某种原因,当尝试使用添加的描述符对我的数组进行排序时,它崩溃了:

[__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance

我什至尝试更改我的数据模型(添加一个属性作为 Int16 并填充它)但是每次我尝试使用新添加的描述符时应用程序都会崩溃。

描述符很简单: 让 sortDescriptor4 = NSSortDescriptor(key: "the_int16_property", ascending: false, selector: "localizedStandardCompare:")

我很茫然。任何建议都会有所帮助。

谢谢!

localizedStandardCompare:NSString的一种方法 "compare strings as sorted by the Finder".

numerical 核心数据属性(如 "Int 16")的键值编码使用 NSNumber 个实例,并且 class 没有响应到 localizedStandardCompare:.

只需使用默认的 compare: 选择器:

NSSortDescriptor(key: "the_int16_property", ascending: false, selector: "compare:")
// Swift 2.2 or later:
NSSortDescriptor(key: "the_int16_property", ascending: false, selector: #selector(NSNumber.compare(_:)))

或者干脆

NSSortDescriptor(key: "the_int16_property", ascending: false)